g0801_0900.s0885_spiral_matrix_iii.Solution Maven / Gradle / Ivy
package g0801_0900.s0885_spiral_matrix_iii;
// #Medium #Array #Matrix #Simulation #2022_03_28_Time_3_ms_(95.45%)_Space_64_MB_(42.42%)
/**
* 885 - Spiral Matrix III\.
*
* Medium
*
* You start at the cell `(rStart, cStart)` of an `rows x cols` grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
*
* You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all `rows * cols` spaces of the grid.
*
* Return _an array of coordinates representing the positions of the grid in the order you visited them_.
*
* **Example 1:**
*
* 
*
* **Input:** rows = 1, cols = 4, rStart = 0, cStart = 0
*
* **Output:** [[0,0],[0,1],[0,2],[0,3]]
*
* **Example 2:**
*
* 
*
* **Input:** rows = 5, cols = 6, rStart = 1, cStart = 4
*
* **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]]
*
* **Constraints:**
*
* * `1 <= rows, cols <= 100`
* * `0 <= rStart < rows`
* * `0 <= cStart < cols`
**/
@SuppressWarnings("java:S135")
public class Solution {
public int[][] spiralMatrixIII(int rows, int cols, int y, int x) {
int j;
int i = 0;
int moves = 0;
int[][] result = new int[rows * cols][2];
result[0][0] = y;
result[0][1] = x;
i++;
while (i < result.length) {
moves++;
// Move right
if (y < 0 || y >= rows) {
x += moves;
} else {
for (j = 0; j < moves; j++) {
x++;
if (x >= 0 && x < cols && y >= 0 && y < rows) {
result[i][0] = y;
result[i][1] = x;
i++;
}
}
}
if (i >= result.length) {
break;
}
// Move down
if (x < 0 || x >= cols) {
y += moves;
} else {
for (j = 0; j < moves; j++) {
y++;
if (x >= 0 && x < cols && y >= 0 && y < rows) {
result[i][0] = y;
result[i][1] = x;
i++;
}
}
}
if (i >= result.length) {
break;
}
moves++;
// Move left
if (y < 0 || y >= rows) {
x -= moves;
} else {
for (j = 0; j < moves; j++) {
x--;
if (x >= 0 && x < cols && y >= 0 && y < rows) {
result[i][0] = y;
result[i][1] = x;
i++;
}
}
}
if (i >= result.length) {
break;
}
// Move up
if (x < 0 || x >= cols) {
y -= moves;
} else {
for (j = 0; j < moves; j++) {
y--;
if (x >= 0 && x < cols && y >= 0 && y < rows) {
result[i][0] = y;
result[i][1] = x;
i++;
}
}
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy