1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
}