File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func spiralOrder( _ matrix: [ [ Int ] ] ) -> [ Int ] {
3
+ var answer : [ Int ] = [ ]
4
+ var top = 0
5
+ var bottom = matrix. endIndex - 1
6
+ var left = 0
7
+ var right = matrix [ 0 ] . endIndex - 1
8
+ while top <= bottom && left <= right {
9
+ for column in left... right {
10
+ answer. append ( matrix [ top] [ column] )
11
+ }
12
+ top += 1
13
+
14
+ if top <= bottom {
15
+ for row in top... bottom {
16
+ answer. append ( matrix [ row] [ right] )
17
+ }
18
+ }
19
+ right -= 1
20
+
21
+ if top <= bottom {
22
+ for column in stride ( from: right, through: left, by: - 1 ) {
23
+ answer. append ( matrix [ bottom] [ column] )
24
+ }
25
+ }
26
+ bottom -= 1
27
+
28
+ if left <= right {
29
+ for row in stride ( from: bottom, through: top, by: - 1 ) {
30
+ answer. append ( matrix [ row] [ left] )
31
+ }
32
+ }
33
+ left += 1
34
+ }
35
+ return answer
36
+ }
37
+ }
38
+
You can’t perform that action at this time.
0 commit comments