forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
35 lines (34 loc) · 940 Bytes
/
Solution.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
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut x1 = 0;
let mut y1 = 0;
let mut x2 = matrix.len() - 1;
let mut y2 = matrix[0].len() - 1;
let mut result = vec![];
while x1 <= x2 && y1 <= y2 {
for j in y1..=y2 {
result.push(matrix[x1][j]);
}
for i in x1 + 1..=x2 {
result.push(matrix[i][y2]);
}
if x1 < x2 && y1 < y2 {
for j in (y1..y2).rev() {
result.push(matrix[x2][j]);
}
for i in (x1 + 1..x2).rev() {
result.push(matrix[i][y1]);
}
}
x1 += 1;
y1 += 1;
if x2 != 0 {
x2 -= 1;
}
if y2 != 0 {
y2 -= 1;
}
}
return result;
}
}