Skip to content

Commit b980326

Browse files
authored
Create Solution.rs
1 parent 64b25ee commit b980326

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
3+
let mut x1 = 0;
4+
let mut y1 = 0;
5+
let mut x2 = matrix.len() - 1;
6+
let mut y2 = matrix[0].len() - 1;
7+
let mut result = vec![];
8+
9+
while x1 <= x2 && y1 <= y2 {
10+
for j in y1..=y2 {
11+
result.push(matrix[x1][j]);
12+
}
13+
for i in x1 + 1..=x2 {
14+
result.push(matrix[i][y2]);
15+
}
16+
if x1 < x2 && y1 < y2 {
17+
for j in (y1..y2).rev() {
18+
result.push(matrix[x2][j]);
19+
}
20+
for i in (x1 + 1..x2).rev() {
21+
result.push(matrix[i][y1]);
22+
}
23+
}
24+
x1 += 1;
25+
y1 += 1;
26+
if x2 != 0 {
27+
x2 -= 1;
28+
}
29+
if y2 != 0 {
30+
y2 -= 1;
31+
}
32+
}
33+
return result;
34+
}
35+
}

0 commit comments

Comments
 (0)