File tree Expand file tree Collapse file tree 1 file changed +15
-11
lines changed Expand file tree Collapse file tree 1 file changed +15
-11
lines changed Original file line number Diff line number Diff line change @@ -9,30 +9,34 @@ class Solution {
9
9
public List <Integer > spiralOrder (int [][] matrix ) {
10
10
List <Integer > answer = new ArrayList <>();
11
11
int lr = 0 ;
12
- int hr = matrix .length - 1 ;
12
+ int hr = matrix .length - 1 ;
13
13
int lc = 0 ;
14
- int hc = matrix [0 ].length - 1 ;
14
+ int hc = matrix [0 ].length - 1 ;
15
15
16
- while (lr <= hr && lc <= hc ) {
17
- for (int c = lc ; c <= hc && lr <= hr ; c ++) {
16
+ while (lr <= hr && lc <= hc ) {
17
+ for (int c = lc ; c <= hc ; c ++) {
18
18
answer .add (matrix [lr ][c ]);
19
19
}
20
20
lr ++;
21
21
22
- for (int r = lr ; r <= hr && lc <= hc ; r ++) {
22
+ for (int r = lr ; r <= hr ; r ++) {
23
23
answer .add (matrix [r ][hc ]);
24
24
}
25
25
hc --;
26
26
27
- for (int c =hc ; c >=lc && lr <=hr ; c --){
28
- answer .add (matrix [hr ][c ]);
27
+ if (lr <= hr ) {
28
+ for (int c = hc ; c >= lc ; c --) {
29
+ answer .add (matrix [hr ][c ]);
30
+ }
31
+ hr --;
29
32
}
30
- hr --;
31
33
32
- for (int r =hr ; r >=lr && lc <=hc ; r --){
33
- answer .add (matrix [r ][lc ]);
34
+ if (lc <= hc ) {
35
+ for (int r = hr ; r >= lr ; r --) {
36
+ answer .add (matrix [r ][lc ]);
37
+ }
38
+ lc ++;
34
39
}
35
- lc ++;
36
40
}
37
41
return answer ;
38
42
}
You can’t perform that action at this time.
0 commit comments