59. Spiral Matrix II
Given a positive integer n
, generate an n x n
matrix
filled with elements from 1
to n2
in spiral order.
Example 1:

Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1 Output: [[1]]
Constraints:
1 <= n <= 20
Rust Solution
struct Solution;
enum Direction {
Right,
Down,
Left,
Up,
}
impl Solution {
fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
use Direction::*;
let n = n as usize;
let mut res: Vec<Vec<i32>> = vec![vec![0; n]; n];
let mut i = 0;
let mut j = 0;
let mut d = Right;
for k in 1..=n * n {
res[i][j] = k as i32;
match d {
Right => {
if j + 1 < n && res[i][j + 1] == 0 {
j += 1;
} else {
d = Down;
i += 1;
}
}
Down => {
if i + 1 < n && res[i + 1][j] == 0 {
i += 1;
} else {
d = Left;
j -= 1;
}
}
Left => {
if j > 0 && res[i][j - 1] == 0 {
j -= 1;
} else {
d = Up;
i -= 1;
}
}
Up => {
if i > 0 && res[i - 1][j] == 0 {
i -= 1;
} else {
d = Right;
j += 1;
}
}
}
}
res
}
}
#[test]
fn test() {
let n = 3;
let res: Vec<Vec<i32>> = vec_vec_i32![[1, 2, 3], [8, 9, 4], [7, 6, 5]];
assert_eq!(Solution::generate_matrix(n), res);
}
Having problems with this solution? Click here to submit an issue on github.