Skip to content

Commit 7249834

Browse files
authored
feat: add rust solution to lc problem: No.54 (doocs#2135)
1 parent 64b25ee commit 7249834

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

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

+40
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

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

+40
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,46 @@ func spiralOrder(matrix [][]int) (ans []int) {
350350
}
351351
```
352352

353+
### **Rust**
354+
355+
```rust
356+
impl Solution {
357+
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
358+
let mut x1 = 0;
359+
let mut y1 = 0;
360+
let mut x2 = matrix.len() - 1;
361+
let mut y2 = matrix[0].len() - 1;
362+
let mut result = vec![];
363+
364+
while x1 <= x2 && y1 <= y2 {
365+
for j in y1..=y2 {
366+
result.push(matrix[x1][j]);
367+
}
368+
for i in x1 + 1..=x2 {
369+
result.push(matrix[i][y2]);
370+
}
371+
if x1 < x2 && y1 < y2 {
372+
for j in (y1..y2).rev() {
373+
result.push(matrix[x2][j]);
374+
}
375+
for i in (x1 + 1..x2).rev() {
376+
result.push(matrix[i][y1]);
377+
}
378+
}
379+
x1 += 1;
380+
y1 += 1;
381+
if x2 != 0 {
382+
x2 -= 1;
383+
}
384+
if y2 != 0 {
385+
y2 -= 1;
386+
}
387+
}
388+
return result;
389+
}
390+
}
391+
```
392+
353393
### **JavaScript**
354394

355395
```js
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)