-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0059_spiral_matrix_ii.rs
More file actions
60 lines (50 loc) · 1.29 KB
/
s0059_spiral_matrix_ii.rs
File metadata and controls
60 lines (50 loc) · 1.29 KB
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
#![allow(unused)]
pub struct Solution {}
use std::usize::MAX;
impl Solution {
// O(n^2) O(1)
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
let mut ans = vec![vec![0; n as usize]; n as usize];
if n < 1 {
return ans;
}
let (mut r1, mut r2, mut c1, mut c2, mut idx) = (0, ans.len() - 1, 0, ans[0].len() - 1, 1);
while r1 <= r2 && c1 <= c2 && r2 != MAX && c2 != MAX {
// top loop
for c in c1..=c2 {
ans[r1][c] = idx;
idx += 1;
}
for r in r1 + 1..=r2 {
ans[r][c2] = idx;
idx += 1;
}
if r1 < r2 && c1 < c2 {
for c in (c1 + 1..c2).rev() {
ans[r2][c] = idx;
idx += 1;
}
for r in (r1 + 1..=r2).rev() {
ans[r][c1] = idx;
idx += 1;
}
}
r1 += 1;
r2 -= 1;
c1 += 1;
c2 -= 1;
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_54() {
assert_eq!(
Solution::generate_matrix(3),
vec![vec![1, 2, 3], vec![8, 9, 4], vec![7, 6, 5]]
);
}
}