Skip to content

Commit 735681e

Browse files
authored
Update README.md
1 parent 7012dc3 commit 735681e

File tree

1 file changed

+40
-0
lines changed
  • solution/0000-0099/0054.Spiral Matrix

1 file changed

+40
-0
lines changed

solution/0000-0099/0054.Spiral Matrix/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,46 @@ func spiralOrder(matrix [][]int) (ans []int) {
360360
}
361361
```
362362

363+
### **Rust**
364+
365+
```rust
366+
impl Solution {
367+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
368+
let mut x1 = 0;
369+
let mut y1 = 0;
370+
let mut x2 = matrix.len() - 1;
371+
let mut y2 = matrix[0].len() - 1;
372+
let mut result = vec![];
373+
374+
while x1 <= x2 && y1 <= y2 {
375+
for j in y1..=y2 {
376+
result.push(matrix[x1][j]);
377+
}
378+
for i in x1 + 1..=x2 {
379+
result.push(matrix[i][y2]);
380+
}
381+
if x1 < x2 && y1 < y2 {
382+
for j in (y1..y2).rev() {
383+
result.push(matrix[x2][j]);
384+
}
385+
for i in (x1 + 1..x2).rev() {
386+
result.push(matrix[i][y1]);
387+
}
388+
}
389+
x1 += 1;
390+
y1 += 1;
391+
if x2 != 0 {
392+
x2 -= 1;
393+
}
394+
if y2 != 0 {
395+
y2 -= 1;
396+
}
397+
}
398+
return result;
399+
}
400+
}
401+
```
402+
363403
### **JavaScript**
364404

365405
```js

0 commit comments

Comments
 (0)