[LeetCode] 334. Increasing Triplet Subsequence

题:https://leetcode.com/problems/game-of-life/description/

题目

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.”

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):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population…
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

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:

  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

思路

题目大意

一个"0","1"组成的矩阵,其中 "0"代表dead , "1"代表 alive 。进行一次迭代。

  1. 若一个matrix[i][j]为alive (“1”),若他接触alive元素 大于等于2 且 小于等于3。那么他的下一代为alive(“1”),否则下一代为 dead(“0”)。
  2. 若一个matrix[i][j]为dead(“0”),若他接触alive元素 等于3,那么他的下一代为alive(“1”),否则下一代为dead(“0”)。

要求:计算过程要求 space O(1)

解题思路

题目最大的难点是空间复杂度为 O(1)。
我们改变原矩阵上board的数值,board 由二进制两位数组成。
[1][0] ,[0] 若为1表示当前为 “1”,否则为"0"。[1] 若为1表示下一代为 “1”,否则为"0"。

获得 当前是否为1 ,x&1 ==1
获得 下一代是否为1 , x&2 == 2
将下一代置为 1 ,x = x|2

code

class Solution:
    def classify(self, board, n, m, posr, posc):
        neighbors_alives = 0
        for i in range(-1, 2):
            for j in range(-1, 2):
                if i == 0 and j == 0:
                    continue
                elif posr + i >= 0 and posr + i < n and posc + j >= 0 and posc + j < m and board[posr+i][posc+j] & 1:
                    neighbors_alives += 1
        return neighbors_alives

    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        n = len(board)
        m = len(board[0])
        for i in range(n):
            for j in range(m):
                stat = self.classify(board, n, m, i, j)
                if board[i][j] & 1:
                    if 2 <= stat <= 3:
                        board[i][j] = board[i][j] | 2
                else:
                    if stat == 3:
                        board[i][j] = board[i][j] | 2
        for i in range(n):
            for j in range(m):
                if board[i][j] & 2:
                    board[i][j] = 1
                else:
                    board[i][j] = 0

猜你喜欢

转载自blog.csdn.net/u013383813/article/details/82902218
今日推荐