Skip to content

Commit 49f392a

Browse files
committed
feat: add solutions to lc problem: No.0289
No.0289.Game of Life
1 parent 11f6414 commit 49f392a

File tree

6 files changed

+518
-40
lines changed

6 files changed

+518
-40
lines changed

solution/0200-0299/0289.Game of Life/README.md

+219-1
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,245 @@
5555
<li>本题中,我们使用二维数组来表示面板。原则上,面板是无限的,但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题?</li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
6261

62+
由于细胞的存活状态变化是同时进行的,因此不能直接遍历格子,依次修改每个细胞的状态,否则前面细胞的状态变化将会影响到下一个要遍历到的细胞的状态。
63+
64+
因此,可以复制 board 样本,每次判断复制样本 cb 中的每个格子,然后对应修改 board 每个细胞的状态。空间复杂度复杂度 `O(mn)`
65+
66+
也可以多定义两个状态,`status = 2` 表示从活细胞转死细胞,`status = 3` 表示从死细胞转活细胞。最后将 `status = 2` 的细胞状态置为 0,而将 `status = 3` 的细胞状态置为 1。空间复杂度 `O(1)`
67+
6368
<!-- tabs:start -->
6469

6570
### **Python3**
6671

6772
<!-- 这里可写当前语言的特殊实现逻辑 -->
6873

6974
```python
75+
class Solution:
76+
def gameOfLife(self, board: List[List[int]]) -> None:
77+
"""
78+
Do not return anything, modify board in-place instead.
79+
"""
80+
m, n = len(board), len(board[0])
81+
cb = [[board[i][j] for j in range(n)] for i in range(m)]
82+
dirs = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]]
83+
for i in range(m):
84+
for j in range(n):
85+
cnt = sum(cb[i + a][j + b] for a, b in dirs if 0 <= i + a < m and 0 <= j + b < n)
86+
if cb[i][j] == 1 and (cnt < 2 or cnt > 3):
87+
board[i][j] = 0
88+
elif cb[i][j] == 0 and (cnt == 3):
89+
board[i][j] = 1
90+
```
7091

92+
```python
93+
class Solution:
94+
def gameOfLife(self, board: List[List[int]]) -> None:
95+
"""
96+
Do not return anything, modify board in-place instead.
97+
"""
98+
m, n = len(board), len(board[0])
99+
dirs = [[-1, 0], [1, 0], [0, -1], [0, 1],
100+
[-1, -1], [-1, 1], [1, -1], [1, 1]]
101+
for i in range(m):
102+
for j in range(n):
103+
cnt = sum(1 for a, b in dirs if 0 <= i + a < m and 0 <=
104+
j + b < n and board[i + a][j + b] in (1, 2))
105+
if board[i][j] == 1 and (cnt < 2 or cnt > 3):
106+
# 活细胞转死细胞
107+
board[i][j] = 2
108+
elif board[i][j] == 0 and (cnt == 3):
109+
# 死细胞转活细胞
110+
board[i][j] = 3
111+
for i in range(m):
112+
for j in range(n):
113+
board[i][j] %= 2
71114
```
72115

73116
### **Java**
74117

75118
<!-- 这里可写当前语言的特殊实现逻辑 -->
76119

77120
```java
121+
class Solution {
122+
public void gameOfLife(int[][] board) {
123+
int m = board.length, n = board[0].length;
124+
int[][] cb = new int[m][n];
125+
for (int i = 0; i < m; ++i) {
126+
for (int j = 0; j < n; ++j) {
127+
cb[i][j] = board[i][j];
128+
}
129+
}
130+
int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
131+
for (int i = 0; i < m; ++i) {
132+
for (int j = 0; j < n; ++j) {
133+
int cnt = 0;
134+
for (int[] dir : dirs) {
135+
int x = i + dir[0], y = j + dir[1];
136+
if (x >= 0 && x < m && y >= 0 && y < n) {
137+
cnt += cb[x][y];
138+
}
139+
}
140+
if (cb[i][j] == 1 && (cnt < 2 || cnt > 3)) {
141+
board[i][j] = 0;
142+
} else if (cb[i][j] == 0 && cnt == 3) {
143+
board[i][j] = 1;
144+
}
145+
}
146+
}
147+
}
148+
}
149+
```
150+
151+
```java
152+
class Solution {
153+
public void gameOfLife(int[][] board) {
154+
int m = board.length, n = board[0].length;
155+
int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
156+
for (int i = 0; i < m; ++i) {
157+
for (int j = 0; j < n; ++j) {
158+
int cnt = 0;
159+
for (int[] dir : dirs) {
160+
int x = i + dir[0], y = j + dir[1];
161+
if (x >= 0 && x < m && y >= 0 && y < n && (board[x][y] == 1 || board[x][y] == 2)) {
162+
++cnt;
163+
}
164+
}
165+
if (board[i][j] == 1 && (cnt < 2 || cnt > 3)) {
166+
board[i][j] = 2;
167+
} else if (board[i][j] == 0 && cnt == 3) {
168+
board[i][j] = 3;
169+
}
170+
}
171+
}
172+
for (int i = 0; i < m; ++i) {
173+
for (int j = 0; j < n; ++j) {
174+
board[i][j] %= 2;
175+
}
176+
}
177+
}
178+
}
179+
```
180+
181+
### **C++**
182+
183+
```cpp
184+
class Solution {
185+
public:
186+
void gameOfLife(vector<vector<int>>& board) {
187+
int m = board.size(), n = board[0].size();
188+
vector<vector<int>> cb(m, vector<int>(n, 0));
189+
for (int i = 0; i < m; ++i)
190+
for (int j = 0; j < n; ++j)
191+
cb[i][j] = board[i][j];
192+
193+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
194+
for (int i = 0; i < m; ++i)
195+
{
196+
for (int j = 0; j < n; ++j)
197+
{
198+
int cnt = 0;
199+
for (auto& dir : dirs)
200+
{
201+
int x = i + dir[0], y = j + dir[1];
202+
if (x >= 0 && x < m && y >= 0 && y < n) cnt += cb[x][y];
203+
}
204+
if (cb[i][j] == 1 && (cnt < 2 || cnt > 3)) board[i][j] = 0;
205+
else if (cb[i][j] == 0 && cnt == 3) board[i][j] = 1;
206+
}
207+
}
208+
}
209+
};
210+
```
211+
212+
```cpp
213+
class Solution {
214+
public:
215+
void gameOfLife(vector<vector<int>>& board) {
216+
int m = board.size(), n = board[0].size();
217+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
218+
for (int i = 0; i < m; ++i)
219+
{
220+
for (int j = 0; j < n; ++j)
221+
{
222+
int cnt = 0;
223+
for (auto& dir : dirs)
224+
{
225+
int x = i + dir[0], y = j + dir[1];
226+
if (x >= 0 && x < m && y >= 0 && y < n && (board[x][y] == 1 || board[x][y] == 2)) ++cnt;
227+
}
228+
if (board[i][j] == 1 && (cnt < 2 || cnt > 3)) board[i][j] = 2;
229+
else if (board[i][j] == 0 && cnt == 3) board[i][j] = 3;
230+
}
231+
}
232+
for (int i = 0; i < m; ++i)
233+
for (int j = 0; j < n; ++j)
234+
board[i][j] %= 2;
235+
}
236+
};
237+
```
238+
239+
### **Go**
240+
241+
```go
242+
func gameOfLife(board [][]int) {
243+
m, n := len(board), len(board[0])
244+
cb := make([][]int, m)
245+
for i := range cb {
246+
cb[i] = make([]int, n)
247+
for j := 0; j < n; j++ {
248+
cb[i][j] = board[i][j]
249+
}
250+
}
251+
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
252+
for i := 0; i < m; i++ {
253+
for j := 0; j < n; j++ {
254+
cnt := 0
255+
for _, dir := range dirs {
256+
x, y := i + dir[0], j + dir[1]
257+
if x >= 0 && x < m && y >= 0 && y < n {
258+
cnt += cb[x][y]
259+
}
260+
}
261+
if cb[i][j] == 1 && (cnt < 2 || cnt > 3) {
262+
board[i][j] = 0
263+
} else if cb[i][j] == 0 && cnt == 3 {
264+
board[i][j] = 1
265+
}
266+
}
267+
}
268+
}
269+
```
78270

271+
```go
272+
func gameOfLife(board [][]int) {
273+
m, n := len(board), len(board[0])
274+
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
275+
for i := 0; i < m; i++ {
276+
for j := 0; j < n; j++ {
277+
cnt := 0
278+
for _, dir := range dirs {
279+
x, y := i+dir[0], j+dir[1]
280+
if x >= 0 && x < m && y >= 0 && y < n && (board[x][y] == 1 || board[x][y] == 2) {
281+
cnt++
282+
}
283+
}
284+
if board[i][j] == 1 && (cnt < 2 || cnt > 3) {
285+
board[i][j] = 2
286+
} else if board[i][j] == 0 && cnt == 3 {
287+
board[i][j] = 3
288+
}
289+
}
290+
}
291+
for i := 0; i < m; i++ {
292+
for j := 0; j < n; j++ {
293+
board[i][j] %= 2
294+
}
295+
}
296+
}
79297
```
80298

81299
### **...**

0 commit comments

Comments
 (0)