Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a6e405

Browse files
authored
Merge pull requestneetcode-gh#168 from r1cky0/patch-14
Create 54-Spiral-Matrix.java
2 parents8f8fbbb +a94e912 commit2a6e405

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎java/54-Spiral-Matrix.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
classSolution {
2+
publicList<Integer>spiralOrder(int[][]matrix) {
3+
List<Integer>list =newArrayList<>();
4+
intrb =0;
5+
intre =matrix.length -1;
6+
intcb =0;
7+
intce =matrix[0].length -1;
8+
9+
while (rb <=re &&cb <=ce) {
10+
for (intj =cb;j <=ce;j++) {
11+
list.add(matrix[rb][j]);
12+
}
13+
rb++;
14+
15+
for (inti =rb;i <=re;i++) {
16+
list.add(matrix[i][ce]);
17+
}
18+
ce--;
19+
20+
if (rb <=re) {
21+
for (intj =ce;j >=cb;j--) {
22+
list.add(matrix[re][j]);
23+
}
24+
}
25+
re--;
26+
27+
if (cb <=ce) {
28+
for (inti =re;i >=rb;i--) {
29+
list.add(matrix[i][cb]);
30+
}
31+
}
32+
cb++;
33+
}
34+
35+
returnlist;
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp