-
Notifications
You must be signed in to change notification settings - Fork 0
/
289.swift
46 lines (40 loc) · 1.44 KB
/
289.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
class Solution {
func gameOfLife(_ board: inout [[Int]]) {
// in-place: 每一位表示一个时间的状态,64位可以表示64个状态
let rows = board.count
let cols = board[0].count
func aliveNow(_ x: Int, _ y: Int) -> Bool {
guard (0..<rows).contains(x), (0..<cols).contains(y) else {
return false
}
return board[x][y] % 2 == 1
}
func willBeAlive(_ x: Int, _ y: Int) -> Bool {
let neighbours = [(x-1, y-1),
(x, y-1),
(x+1, y-1),
(x-1, y),
(x+1, y),
(x-1, y+1),
(x, y+1),
(x+1, y+1)]
var aliveNeighbours = 0
for (x, y) in neighbours where aliveNow(x, y) {
aliveNeighbours += 1
}
let isAliveNow = aliveNow(x, y)
return (2...3).contains(aliveNeighbours) && isAliveNow ||
aliveNeighbours == 3 && !isAliveNow
}
for x in board.indices {
for y in board[0].indices where willBeAlive(x, y) {
board[x][y] += 2
}
}
for x in board.indices {
for y in board[0].indices {
board[x][y] /= 2
}
}
}
}