Skip to content

Commit

Permalink
Create 0054-spiral-matrix.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
drxlx committed Jun 19, 2024
1 parent 32bb838 commit 43d922d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions swift/0054-spiral-matrix.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Question Link: https://leetcode.com/problems/spiral-matrix/
*/

class Solution {
func spiralOrder(_ matrix: [[Int]]) -> [Int] {
var res = [Int]()
var l = 0
var r = matrix[0].count
var t = 0
var b = matrix.count
while l < r && t < b {
for i in l..<r {
res.append(matrix[t][i])
}
t += 1
for i in t..<b {
res.append(matrix[i][r - 1])
}
r -= 1
if !(l < r && t < b) {
break
}
for i in stride(from: r - 1, to: l - 1, by: -1) {
res.append(matrix[b - 1][i])
}
b -= 1
for i in stride(from: b - 1, to: t - 1, by: -1) {
res.append(matrix[i][l])
}
l += 1
}
return res
}
}

0 comments on commit 43d922d

Please sign in to comment.