Skip to content

Commit fa9de70

Browse files
refactor 885
1 parent a6ac782 commit fa9de70

File tree

1 file changed

+8
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+8
-13
lines changed

src/main/java/com/fishercoder/solutions/_885.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,22 @@ public class _885 {
2828
public static class Solution1 {
2929
/**credit: https://leetcode.com/problems/spiral-matrix-iii/discuss/158977/Java-15-lines-concise-solution-with-comments*/
3030
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
31-
int[][] directions = new int[][]{
32-
{0, 1},
33-
{1, 0},
34-
{0, -1},
35-
{-1, 0}
36-
};
31+
int[] directions = new int[]{0, 1, 0, -1, 0};
3732
int[][] result = new int[R * C][2];
38-
int j = 0;
39-
result[j++] = new int[]{r0, c0};
33+
int i = 0;
34+
result[i++] = new int[]{r0, c0};
4035
int len = 0;
4136
int d = 0;
42-
while (j < R * C) {
37+
while (i < R * C) {
4338
if (d == 0 || d == 2) {
4439
//plus one when moving east or west
4540
len++;
4641
}
47-
for (int i = 0; i < len; i++) {
48-
r0 += directions[d][0];
49-
c0 += directions[d][1];
42+
for (int k = 0; k < len; k++) {
43+
r0 += directions[d];
44+
c0 += directions[d + 1];
5045
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
51-
result[j++] = new int[]{r0, c0};
46+
result[i++] = new int[]{r0, c0};
5247
}
5348
}
5449
d = (d + 1) % 4;

0 commit comments

Comments
 (0)