-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0059.Spiral Matrix II.swift
86 lines (74 loc) · 2.02 KB
/
0059.Spiral Matrix II.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Solution {
private var num = 1
private var n = 0
private var matrix = [[Int]]()
func generateMatrix(_ n: Int) -> [[Int]] {
self.n = n
self.matrix = Array(repeating: Array(repeating: -1, count: n), count: n)
let finalValue = n * n
var direction = Direction.none
var cell = (0, 0)
while num <= finalValue {
direction = direction.next()
cell = fillLine(cell, direction)
}
return matrix
}
private func fillLine(_ from: (row: Int, col: Int), _ direction: Direction) -> (Int, Int) {
var col = from.col
var row = from.row
print(direction, row, col)
while row >= 0, row < n, col >= 0, col < n, matrix[row][col] == -1 {
matrix[row][col] = num
num += 1
if direction == .right {
col += 1
} else if direction == .down {
row += 1
} else if direction == .left {
col -= 1
} else if direction == .top {
row -= 1
}
}
// get next start cell
switch(direction) {
case .right:
col -= 1
row += 1
case .down:
col -= 1
row -= 1
case .left:
col += 1
row -= 1
case .top:
col += 1
row += 1
default:
print("Must never happen")
}
return (row, col)
}
enum Direction {
case none
case right
case down
case left
case top
func next() -> Direction {
switch self {
case .none:
return .right
case .right:
return .down
case .down:
return .left
case .left:
return .top
case .top:
return .right
}
}
}
}