File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * <a href="https://leetcode.com/problems/spiral-matrix/">week06-5.spiral-matrix</a>
3
+ * <li>Description: return all elements of the matrix in spiral order</li>
4
+ * <li>Topics: Array, Matrix, Simulation </li>
5
+ * <li>Time Complexity: O(N*M), Runtime 0ms </li>
6
+ * <li>Space Complexity: O(1), Memory 41.95MB </li>
7
+ */
8
+ class Solution {
9
+ public List <Integer > spiralOrder (int [][] matrix ) {
10
+ List <Integer > answer = new ArrayList <>();
11
+ int lr = 0 ;
12
+ int hr = matrix .length -1 ;
13
+ int lc = 0 ;
14
+ int hc = matrix [0 ].length -1 ;
15
+
16
+ while (lr <=hr && lc <=hc ) {
17
+ for (int c =lc ; c <=hc && lr <=hr ; c ++) {
18
+ answer .add (matrix [lr ][c ]);
19
+ }
20
+ lr ++;
21
+
22
+ for (int r =lr ; r <=hr && lc <=hc ; r ++) {
23
+ answer .add (matrix [r ][hc ]);
24
+ }
25
+ hc --;
26
+
27
+ for (int c =hc ; c >=lc && lr <=hr ; c --){
28
+ answer .add (matrix [hr ][c ]);
29
+ }
30
+ hr --;
31
+
32
+ for (int r =hr ; r >=lr && lc <=hc ; r --){
33
+ answer .add (matrix [r ][lc ]);
34
+ }
35
+ lc ++;
36
+ }
37
+ return answer ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments