We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 64b25ee commit b980326Copy full SHA for b980326
solution/0000-0099/0054.Spiral Matrix/Solution.rs
@@ -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