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

Commitc12b8c2

Browse files
add a solution for 54
1 parent6f4542b commitc12b8c2

File tree

1 file changed

+51
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+51
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_54.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,55 @@ public List<Integer> spiralOrder(int[][] matrix) {
4141
returnresult;
4242
}
4343
}
44+
45+
publicstaticclassSolution2 {
46+
/**
47+
* My completely original solution on 12/29/2021.
48+
*/
49+
publicList<Integer>spiralOrder(int[][]matrix) {
50+
List<Integer>ans =newArrayList<>();
51+
intm =matrix.length;
52+
intn =matrix[0].length;
53+
intdirection =0;
54+
inttotal =m *n;
55+
intj =0;
56+
inti =0;
57+
intlowerRow =0;
58+
intlowerCol =0;
59+
while (ans.size() <total) {
60+
for (;i <m &&i >=lowerRow &&j <n &&j >=lowerCol; ) {
61+
ans.add(matrix[i][j]);
62+
if (direction ==0) {//east
63+
j++;
64+
}elseif (direction ==1) {//south
65+
i++;
66+
}elseif (direction ==2) {//west
67+
j--;
68+
}else {
69+
i--;//north
70+
}
71+
}
72+
if (direction ==0) {
73+
i++;
74+
j--;
75+
}elseif (direction ==1) {
76+
j--;
77+
i--;
78+
}elseif (direction ==2) {
79+
j++;
80+
i--;
81+
lowerRow++;
82+
m--;
83+
}else {
84+
i++;
85+
j++;
86+
lowerCol++;
87+
n--;
88+
}
89+
direction++;
90+
direction %=4;
91+
}
92+
returnans;
93+
}
94+
}
4495
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp