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 @@ -360,6 +360,46 @@ func spiralOrder(matrix [][]int) (ans []int) {
360
360
}
361
361
```
362
362
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
+
363
403
### ** JavaScript**
364
404
365
405
``` js
You can’t perform that action at this time.
0 commit comments