File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ class Solution {
3
+ public List <Integer > spiralOrder (int [][] matrix ) {
4
+
5
+ List <Integer > result = new ArrayList <>();
6
+
7
+ int left = 0 ;
8
+ int right = matrix [0 ].length - 1 ;
9
+ int top = 0 ;
10
+ int bottom = matrix .length - 1 ;
11
+
12
+ while (left <= right && top <= bottom ) {
13
+
14
+ // ์์ชฝ ํ์ ์ํํ ํ, ์๋จ ๊ฒฝ๊ณ๋ฅผ 1 ์ฆ๊ฐ
15
+ for (int y = left ; y <= right ; y ++) {
16
+ result .add (matrix [top ][y ]);
17
+ }
18
+ top ++;
19
+
20
+ // ์๋จ ์ธ๋ฑ์ค๊ฐ ํ๋จ ์ธ๋ฑ์ค ๋ณด๋ค ์ปค์ง๋ฉด ์ํ ์ค๋จ
21
+ if (top > bottom ) break ;
22
+
23
+ // ์ฐ์ธก ์ด์ ์ํํ ํ, ์ฐ์ธก ๊ฒฝ๊ณ๋ฅผ 1 ๊ฐ์
24
+ for (int x = top ; x <= bottom ; x ++) {
25
+ result .add (matrix [x ][right ]);
26
+ }
27
+ right --;
28
+
29
+ // ์ฐ์ธก ์ธ๋ฑ์ค๊ฐ ์ข์ธก ์ธ๋ฑ์ค๋ณด๋ค ์์์ง๋ฉด ์ํ ์ค๋จ
30
+ if (right < left ) break ;
31
+
32
+ // ์๋์ชฝ ํ์ ์ํํ ํ, ํ๋จ ๊ฒฝ๊ณ๋ฅผ 1 ๊ฐ์
33
+ for (int y = right ; y >= left ; y --) {
34
+ result .add (matrix [bottom ][y ]);
35
+ }
36
+ bottom --;
37
+
38
+ // ์ผ์ชฝ ์ด์ ์ํํ ํ, ์ข์ธก ๊ฒฝ๊ณ๋ฅผ 1 ์ฆ๊ฐ
39
+ for (int x = bottom ; x >= top ; x --) {
40
+ result .add (matrix [x ][left ]);
41
+ }
42
+ left ++;
43
+
44
+ }
45
+ return result ;
46
+
47
+ }
48
+
49
+ }
You canโt perform that action at this time.
0 commit comments