Skip to main content
Back to problems
#885
Medium Algorithms

Spiral matrix iii

Array Matrix Simulation
84.5% acceptance
Feb 22, 2026
1661
1045
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.

Solution

Rust
Time O(n³)
Space O(n)
LeetCode
solution.rs
/*
 * 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
 */

impl Solution {
  pub fn spiral_matrix_iii(rows: i32, cols: i32, r_start: i32, c_start: i32) -> Vec<Vec<i32>> {
    let total = (rows * cols) as usize;
    let mut res = Vec::with_capacity(total);
    let (mut r, mut c) = (r_start, c_start);
    // directions: E, S, W, N
    let dr = [0i32, 1, 0, -1];
    let dc = [1i32, 0, -1, 0];
    let mut dir = 0usize;
    let mut steps = 1i32;
    while res.len() < total {
      for _ in 0..2 {
        for _ in 0..steps {
          if r >= 0 && r < rows && c >= 0 && c < cols {
            res.push(vec![r, c]);
          }
          r += dr[dir]; c += dc[dir];
        }
        dir = (dir + 1) % 4;
      }
      steps += 1;
    }
    res
  }
}