-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.rs
38 lines (33 loc) · 1016 Bytes
/
Solution2.rs
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
impl Solution {
pub fn spiral_order(mut matrix: Vec<Vec<i32>>) -> Vec<i32> {
let m = matrix.len();
let n = matrix[0].len();
let mut dirs = vec![0, 1, 0, -1, 0];
let mut i = 0;
let mut j = 0;
let mut k = 0;
let mut ans = Vec::new();
for _ in 0..(m * n) {
ans.push(matrix[i][j]);
matrix[i][j] += 300;
let x = i as i32 + dirs[k] as i32;
let y = j as i32 + dirs[k + 1] as i32;
if x < 0
|| x >= m as i32
|| y < 0
|| y >= n as i32
|| matrix[x as usize][y as usize] > 100
{
k = (k + 1) % 4;
}
i = (i as i32 + dirs[k] as i32) as usize;
j = (j as i32 + dirs[k + 1] as i32) as usize;
}
for i in 0..m {
for j in 0..n {
matrix[i][j] -= 300;
}
}
ans
}
}