Skip to content

Commit 12fc06f

Browse files
committed
solve problem
1 parent ef1712e commit 12fc06f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

spiral-matrix/delight010.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+

0 commit comments

Comments
 (0)