这道题的 in-place 解法的核心思路是编码一个状态码,存储当前位置的当前状态和下一步状态。遍历一次更新了状态码之后再解码。
class Solution:
@classmethod
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
for i in range(len(board)):
for j in range(len(board[i])):
neighbour_count = 0
if i > 0:
neighbour_count += board[i-1][j] % 2 # upper center
if j > 0:
neighbour_count += board[i-1][j-1] % 2 # upper left
if j < len(board[i]) - 1:
neighbour_count += board[i-1][j+1] % 2 # upper right
if i < len(board) - 1:
neighbour_count += board[i+1][j] # lower center
if j > 0:
neighbour_count += board[i+1][j-1] # lower left
if j < len(board[i]) - 1:
neighbour_count += board[i+1][j+1] # lower right
if j > 0:
neighbour_count += board[i][j-1] % 2 # middle left
if j < len(board[i]) - 1:
neighbour_count += board[i][j+1] # middle right
if board[i][j] == 0 and neighbour_count == 3:
board[i][j] = 2
if board[i][j] == 1:
if neighbour_count == 3 or neighbour_count == 2:
board[i][j] = 3
for i in range(len(board)):
for j in range(len(board[i])):
board[i][j] //= 2
return
文章的作者头两年在 Google 表现得很出色,得到了上司给予的“远超期望”的评价,本来以为自己可以很快升职加薪~~、当上ceo、迎娶白富美、走上人生巅峰~~了。没想到提交了几次考核,总是没有得到升职委员会的肯定,再加上自己负责的几个项目总是在做了几个月之后被移交给姊妹团队,心态最后崩溃一走了之。
诚然,作为一个有追求的软件开发工程师,能开发出高效、美观且易维护的代码是很理想的。但 PM 怎么受得了一个项目做了几个月还在不停地设计、抽象呢?
所以面对项目开发时间和代码质量之间的矛盾,敏捷开发是个挺不错的思路。
Markdown 的删除线 ~~xxx~~
这周过中秋,下周一定写🤣