Skip to content

Commit 8d74402

Browse files
add 885
1 parent ac413c9 commit 8d74402

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ _If you like this project, please leave me a star._ ★
106106
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
107107
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java) | O(n) | O(1) | |Medium|
108108
|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_888.java) | O(n) | O(1) | |Easy|
109+
|885|[Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_885.java) | | | |Medium|
109110
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
110111
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
111112
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | O(n) | O(h) | |Easy| DFS, recursion
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 885. Spiral Matrix III
5+
*
6+
* On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east.
7+
* Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.
8+
* Now, we walk in a clockwise spiral shape to visit every position in this grid.
9+
* Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)
10+
* Eventually, we reach all R * C spaces of the grid.
11+
* Return a list of coordinates representing the positions of the grid in the order they were visited.
12+
*
13+
* Example 1:
14+
* Input: R = 1, C = 4, r0 = 0, c0 = 0
15+
* Output: [[0,0],[0,1],[0,2],[0,3]]
16+
*
17+
* Example 2:
18+
* Input: R = 5, C = 6, r0 = 1, c0 = 4
19+
* Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
20+
*
21+
* Note:
22+
* 1 <= R <= 100
23+
* 1 <= C <= 100
24+
* 0 <= r0 < R
25+
* 0 <= c0 < C
26+
* */
27+
public class _885 {
28+
public static class Solution1 {
29+
/**credit: https://leetcode.com/problems/spiral-matrix-iii/discuss/158977/Java-15-lines-concise-solution-with-comments*/
30+
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+
};
37+
int[][] result = new int[R * C][2];
38+
int j = 0;
39+
result[j++] = new int[]{r0, c0};
40+
int len = 0;
41+
int d = 0;
42+
while (j < R * C) {
43+
if (d == 0 || d == 2) {
44+
//plus one when moving east or west
45+
len++;
46+
}
47+
for (int i = 0; i < len; i++) {
48+
r0 += directions[d][0];
49+
c0 += directions[d][1];
50+
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
51+
result[j++] = new int[]{r0, c0};
52+
}
53+
}
54+
d = (d + 1) % 4;
55+
}
56+
return result;
57+
}
58+
}
59+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._885;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _885Test {
10+
private static _885.Solution1 solution1;
11+
private static int[][] expected;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _885.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
expected = new int[][]{
21+
{0, 0},
22+
{0, 1},
23+
{0, 2},
24+
{0, 3}
25+
};
26+
assertArrayEquals(expected, solution1.spiralMatrixIII(1, 4, 0, 0));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
expected = new int[][]{
32+
{1, 4},
33+
{1, 5},
34+
{2, 5},
35+
{2, 4},
36+
{2, 3},
37+
{1, 3},
38+
{0, 3},
39+
{0, 4},
40+
{0, 5},
41+
{3, 5},
42+
{3, 4},
43+
{3, 3},
44+
{3, 2},
45+
{2, 2},
46+
{1, 2},
47+
{0, 2},
48+
{4, 5},
49+
{4, 4},
50+
{4, 3},
51+
{4, 2},
52+
{4, 1},
53+
{3, 1},
54+
{2, 1},
55+
{1, 1},
56+
{0, 1},
57+
{4, 0},
58+
{3, 0},
59+
{2, 0},
60+
{1, 0},
61+
{0, 0}
62+
};
63+
assertArrayEquals(expected, solution1.spiralMatrixIII(5, 6, 1, 4));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)