Class Solution

java.lang.Object
g0801_0900.s0885_spiral_matrix_iii.Solution

public class Solution extends Object
885 - Spiral Matrix III.<p>Medium</p> <p>You start at the cell <code>(rStart, cStart)</code> of an <code>rows x cols</code> 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.</p> <p>You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid&rsquo;s boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all <code>rows * cols</code> spaces of the grid.</p> <p>Return <em>an array of coordinates representing the positions of the grid in the order you visited them</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_1.png" alt="" /></p> <p><strong>Input:</strong> rows = 1, cols = 4, rStart = 0, cStart = 0</p> <p><strong>Output:</strong> [[0,0],[0,1],[0,2],[0,3]]</p> <p><strong>Example 2:</strong></p> <p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_2.png" alt="" /></p> <p><strong>Input:</strong> rows = 5, cols = 6, rStart = 1, cStart = 4</p> <p><strong>Output:</strong> [[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]]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= rows, cols <= 100</code></li> <li><code>0 <= rStart < rows</code></li> <li><code>0 <= cStart < cols</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • spiralMatrixIII

      public int[][] spiralMatrixIII(int rows, int cols, int y, int x)