289._game_of_life.md 3.3 KB
Newer Older
K
KEQI HUANG 已提交
1
### 289. Game of Life
K
KEQI HUANG 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

题目: 
<https://leetcode.com/problems/game-of-life/>


难度 : Medium


直接一上来就没有考虑solve it in-place,考虑的是便利,简直是born for 便利

首先我把board拓宽了,宽,高各增加了两排。

因为这样求neighbor方便,针对原来的borad,现在新的big 对于 1 -> n-1 的部分

全都有八个neighbor,用了一个2d array来记录nbrs,再根据当下的nbr来判断更新,因为不能一边在board上loop一边更新.

AC的效率还ok:

K
KEQI HUANG 已提交
20
```python
K
KEQI HUANG 已提交
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
class Solution(object):
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        def liveNeighbors(i,j):
            return big[i-1][j-1] + big[i-1][j] + big[i-1][j+1] + big[i][j-1] + big[i][j+1] + big[i+1][j-1] + big[i+1][j] + big[i+1][j+1]
        
        if board == [[]] : return
        row = len(board)
        col = len(board[0]) 

        nbrs = [[0 for j in range(col)] for i in range(row)]
        big = [[ 0 for j in range(col+2) ] for i in range(row+2)]
        for i in range(1,row+1):
            for j in range(1,col+1):
                big[i][j] = board[i-1][j-1]
            
        for i in range(1,row+1):
            for j in range(1,col+1):
                nbrs[i-1][j-1] = liveNeighbors(i,j)
    
        for i in range(row):
            for j in range(col):
                if board[i][j] == 1:                
                    if nbrs[i][j] < 2:
                        board[i][j] = 0
                    elif nbrs[i][j] == 2 or nbrs[i][j] == 3:
                        board[i][j] = 1
                    else:
                        board[i][j] = 0
                else:
                    if nbrs[i][j] == 3:
                        board[i][j] = 1
            
```

谷歌了一下,大家都用到了temp 2d array嘛,哼(ˉ(∞)ˉ)唧。好吧,空间复杂度比我小。



很多的解法都是一样开了一个二维数组,即使没有像我一样扩展board.因为问题在于不能一边更新board 一边来做。

看了一下这边的思路:

<https://www.hrwhisper.me/leetcode-game-of-life/>

<http://www.cnblogs.com/grandyang/p/4854466.html>



不开数组

我们可以使用状态机转换 o(╯□╰)o  感觉不知道在听什么 还是很迷茫的感觉, in-place AC代码

K
KEQI HUANG 已提交
77
```python
K
KEQI HUANG 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
class Solution(object):
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        row = len(board)
        col = len(board[0]) if row else 0

        dx = [-1,-1,-1,0,1,1,1,0]
        dy = [-1,0,1,1,1,0,-1,-1]
    
        for i in range(row):
            for j in range(col):
                cnt = 0
                for k in range(8):
                    x, y = i + dx[k], j + dy[k]
                    if x >=0 and x < row and y >=0 and y < col and (board[x][y] == 1  or board[x][y] == 2):
                        cnt += 1

                if board[i][j] and (cnt < 2 or cnt > 3):
                    board[i][j] = 2
                elif board[i][j] == 0 and cnt == 3:
                    board[i][j] = 3

        for i in range(row):
            for j in range(col):
                board[i][j] %= 2
                   
   
```