★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qvrgpcjv-kw.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."git
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):github
Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.算法
Example:數組
Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[ [0,0,0], [1,0,1], [0,1,1], [0,1,0] ]
Follow up:微信
根據百度百科,生命遊戲,簡稱爲生命,是英國數學家約翰·何頓·康威在1970年發明的細胞自動機。app
給定一個包含 m × n 個格子的面板,每個格子均可以當作是一個細胞。每一個細胞具備一個初始狀態 live(1)即爲活細胞, 或 dead(0)即爲死細胞。每一個細胞與其八個相鄰位置(水平,垂直,對角線)的細胞都遵循如下四條生存定律:函數
根據當前狀態,寫一個函數來計算面板上細胞的下一個(一次更新後的)狀態。下一個狀態是經過將上述規則同時應用於當前狀態下的每一個細胞所造成的,其中細胞的出生和死亡是同時發生的。this
示例:spa
輸入: [ [0,1,0], [0,0,1], [1,1,1], [0,0,0] ] 輸出: [ [0,0,0], [1,0,1], [0,1,1], [0,1,0] ]
進階:
12ms
1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 4 let m = board.count 5 let n = board[0].count 6 7 for i in 0..<m { 8 for j in 0..<n { 9 var count = 0 10 if i != 0 { 11 if j != 0 { 12 count += board[i-1][j-1] > 0 ? 1 : 0 13 } 14 15 if j != n-1 { 16 count += board[i-1][j+1] > 0 ? 1 : 0 17 } 18 19 count += board[i-1][j] > 0 ? 1 : 0 20 } 21 22 if i != m-1 { 23 if j != 0 { 24 count += board[i+1][j-1] > 0 ? 1 : 0 25 } 26 27 if j != n-1 { 28 count += board[i+1][j+1] > 0 ? 1 : 0 29 } 30 31 count += board[i+1][j] > 0 ? 1 : 0 32 } 33 34 if j != 0 { 35 count += board[i][j-1] > 0 ? 1 : 0 36 } 37 38 if j != n-1 { 39 count += board[i][j+1] > 0 ? 1 : 0 40 } 41 42 if board[i][j] == 0 { 43 if count == 3 { 44 board[i][j] = -1 45 } 46 }else { 47 if count != 2 && count != 3 { 48 board[i][j] = 2 49 } 50 } 51 } 52 } 53 54 for i in 0..<m { 55 for j in 0..<n { 56 if board[i][j] == 2 { 57 board[i][j] = 0 58 } 59 if board[i][j] == -1 { 60 board[i][j] = 1 61 } 62 } 63 } 64 } 65 }
20ms
1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 var m = board.count 4 var n = board[0].count 5 var matrix = board 6 7 func helper(_ i: Int, _ j: Int) -> Int { 8 var count = 0 9 if i > 0 { 10 count += board[i - 1][j] 11 if j > 0 { 12 count += board[i - 1][j - 1] 13 } 14 if j < n - 1 { 15 count += board[i - 1][j + 1] 16 } 17 } 18 if i < m - 1 { 19 count += board[i + 1][j] 20 if j > 0 { 21 count += board[i + 1][j - 1] 22 } 23 if j < n - 1 { 24 count += board[i + 1][j + 1] 25 } 26 } 27 if j > 0 { 28 count += board[i][j - 1] 29 } 30 if j < n - 1 { 31 count += board[i][j + 1] 32 } 33 34 if board[i][j] == 1 { 35 if count == 2 || count == 3 { 36 return 1 37 } else { 38 return 0 39 } 40 } else { 41 if count == 3 { 42 return 1 43 } else { 44 return 0 45 } 46 } 47 } 48 49 for i in 0 ..< m { 50 for j in 0 ..< n { 51 matrix[i][j] = helper(i, j) 52 } 53 } 54 board = matrix 55 } 56 }
32ms
1 class Solution { 2 func gameOfLife(_ board: inout [[Int]]) { 3 guard board.count > 0 else { 4 return 5 } 6 7 let m = board.count, n = board[0].count 8 for i in 0..<m { 9 for j in 0..<n { 10 changeStatus(&board, i, j, m, n) 11 } 12 } 13 board = board.map { $0.map{ $0 % 2 } } 14 print(board) 15 } 16 17 func changeStatus(_ board: inout [[Int]], _ i: Int, _ j: Int, _ m: Int, _ n: Int) { 18 var liveNum = 0 19 20 for x in (i - 1)...(i + 1) { 21 for y in (j - 1)...(j + 1) { 22 if x < 0 || x >= m || y < 0 || y >= n { 23 continue 24 } 25 26 if x == i && y == j { 27 continue 28 } 29 30 liveNum = board[x][y] == 1 || board[x][y] == 2 ? liveNum + 1 : liveNum 31 } 32 } 33 34 if board[i][j] == 1 { 35 board[i][j] = (liveNum < 2 || liveNum > 3) ? 2 : 1 36 } else { 37 board[i][j] = liveNum == 3 ? 3 : 0 38 } 39 } 40 }