Skip to content

Commit 057821e

Browse files
committed
feat : spiral-matrix
1 parent ac9ae51 commit 057821e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

spiral-matrix/ekgns33.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*. lr ->
3+
* ^ 1 2 3 4. rd
4+
* lu 5 6 7 8. v
5+
* <- rl
6+
* tc : O(mn)
7+
* sc : O(1)
8+
* */
9+
10+
class Solution {
11+
public List<Integer> spiralOrder(int[][] matrix) {
12+
int m = matrix.length;
13+
int n = matrix[0].length;
14+
int lr = 0, rd = 0, rl = n -1, lu = m - 1;
15+
int cnt = 0, target = m*n;
16+
List<Integer> res = new ArrayList<>();
17+
18+
while(lr <= rl && rd <= lu) {
19+
for(int startLeft = lr; startLeft <= rl; startLeft++) {
20+
res.add(matrix[rd][startLeft]);
21+
}
22+
rd++;
23+
for(int startUp = rd; startUp <=lu; startUp++) {
24+
res.add(matrix[startUp][rl]);
25+
}
26+
rl--;
27+
if(rd <= lu) {
28+
for(int startRight = rl; startRight >= lr; startRight--) {
29+
res.add(matrix[lu][startRight]);
30+
}
31+
}
32+
lu--;
33+
if(lr <= rl) {
34+
for(int startDown = lu; startDown >= rd; startDown--) {
35+
res.add(matrix[startDown][lr]);
36+
}
37+
}
38+
lr++;
39+
}
40+
return res;
41+
}
42+
}

0 commit comments

Comments
 (0)