forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtedkimdev.rs
More file actions
38 lines (33 loc) · 1.03 KB
/
tedkimdev.rs
File metadata and controls
38 lines (33 loc) · 1.03 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
// TC: O(m * n)
// SC: O(m * n)
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let mut result: Vec<i32> = Vec::new();
let mut left = 0i32;
let mut right = matrix[0].len() as i32;
let mut top = 0i32;
let mut bottom = matrix.len() as i32;
while left < right && top < bottom {
for i in left..right {
result.push(matrix[top as usize][i as usize]);
}
top += 1;
for i in top..bottom {
result.push(matrix[i as usize][(right - 1) as usize]);
}
right -= 1;
if !(left < right && top < bottom) {
break;
}
for i in (left..right).rev() {
result.push(matrix[(bottom - 1) as usize][i as usize]);
}
bottom -= 1;
for i in (top..bottom).rev() {
result.push(matrix[i as usize][left as usize]);
}
left += 1;
}
result
}
}