Skip to content

Commit 7656fce

Browse files
committed
1
1 parent 82548ee commit 7656fce

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

notes/src/day2/lc59.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,44 @@ impl Solution {
6060

6161
可以发现这里的边界条件非常多,在一个循环中,如此多的边界条件,如果不按照固定规则来遍历,那就是一进循环深似海,从此offer是路人。
6262

63-
然后好像就是我这种模拟的做法
63+
然后好像就是我这种模拟的做法
64+
65+
66+
```rust
67+
68+
# struct Solution {}
69+
70+
impl Solution {
71+
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
72+
let mut m: Vec<Vec<i32>> = vec![vec![0i32; n as usize]; n as usize];
73+
let mut current: i32 = 1i32;
74+
for i in 0usize .. (n as usize + 1usize) / 2usize {
75+
Self::f(&mut m, i, &mut current);
76+
}
77+
m
78+
}
79+
80+
pub fn f(m: &mut Vec<Vec<i32>>, start: usize, start_num: &mut i32) {
81+
let width: usize = m.len() - start * 2usize - 1usize;
82+
if width == 0 { m[start][start] = *start_num; return; }
83+
for i in 0..width {
84+
m[start][start + i] = *start_num;
85+
*start_num += 1i32;
86+
}
87+
for j in 0..width {
88+
m[start + j][start + width] = *start_num;
89+
*start_num += 1i32;
90+
}
91+
for i in 0..width {
92+
m[start + width][start + width - i] = *start_num;
93+
*start_num += 1i32;
94+
}
95+
for j in 0..width {
96+
m[start + width - j][start] = *start_num;
97+
*start_num += 1i32;
98+
}
99+
}
100+
}
101+
102+
103+
```

0 commit comments

Comments
 (0)