File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
solution/0000-0099/0054.Spiral Matrix Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -350,6 +350,46 @@ func spiralOrder(matrix [][]int) (ans []int) {
350
350
}
351
351
```
352
352
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
+
353
393
### ** JavaScript**
354
394
355
395
``` js
You can’t perform that action at this time.
0 commit comments