Skip to content

Commit 75556fb

Browse files
committed
leetcode-289
1 parent 0f95483 commit 75556fb

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Array.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ No.|Title|Difficulty|Solved|Date
66
26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|Easy|yes|2019-01-10
77
27|[Remove Element](https://leetcode.com/problems/remove-element/)|Easy|yes|2019-01-10
88
153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|Medium|yes|2019-01-19
9+
289|[Game Of Life](https://leetcode.com/problems/game-of-life/)|Medium|yes|2020-01-11
910
791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|Medium|yes|2020-01-07
1011
----------------------------------------------------------------
1112
1. [Two Sum](https://leetcode.com/problems/two-sum/)
@@ -46,6 +47,12 @@ private int findMin(int[] nums, int start, int end) {
4647
}
4748
```
4849

50+
289. [Game Of Life](https://leetcode.com/problems/game-of-life/)
51+
52+
要求不使用额外变量临时记录中间状态的话,考虑使用特征值的方法。所有的Cell都处理完成之后,再将特征值替换会原始的值。
53+
54+
例如,死亡的Cell -> 存活的Cell,这一过程的特征值为 2。存活的Cell -> 死亡的Cell,这一过程的特征值为 -1。在统计Cell周边存活Cell的个数的时候,需要统计值为-1的Cell。
55+
4956
791. [Custom Sort String](https://leetcode.com/problems/custom-sort-string/)
5057

5158
自定义排序,排序规则是在给定的字符串中。

src/main/java/medium/lc289.java

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package medium;
2+
3+
class lc289 {
4+
// (0 -> 1) -> 2
5+
// (1 -> 0) -> -1
6+
public void gameOfLife(int[][] board) {
7+
8+
for (int i = 0; i < board.length; i++) {
9+
for (int j = 0; j < board[i].length; j++) {
10+
int aliveCount = getAliveCount(board, i, j);
11+
if (board[i][j] == 0) {
12+
if (aliveCount == 3) {
13+
board[i][j] = 2;
14+
}
15+
} else if (board[i][j] == 1) {
16+
if (aliveCount < 2 || aliveCount > 3) {
17+
board[i][j] = -1;
18+
}
19+
}
20+
}
21+
}
22+
23+
for (int i = 0; i < board.length; i++) {
24+
for (int j = 0; j < board[i].length; j++) {
25+
if (board[i][j] == -1) {
26+
board[i][j] = 0;
27+
} else if (board[i][j] == 2) {
28+
board[i][j] = 1;
29+
}
30+
}
31+
}
32+
}
33+
34+
private boolean isAlive(int[][] board, int i, int j) {
35+
return board[i][j] == 1 || board[i][j] == -1;
36+
}
37+
38+
private int getAliveCount(int[][] board, int i, int j) {
39+
int result = 0;
40+
if (i - 1 >= 0 && j - 1 >= 0 && isAlive(board, i - 1, j - 1)) {
41+
result += 1;
42+
}
43+
if (i - 1 >= 0 && isAlive(board, i - 1, j)) {
44+
result += 1;
45+
}
46+
if (i - 1 >= 0 && j + 1 < board[i].length && isAlive(board, i - 1, j + 1)) {
47+
result += 1;
48+
}
49+
if (j - 1 >= 0 && isAlive(board, i, j - 1)) {
50+
result += 1;
51+
}
52+
53+
if (j + 1 < board[i].length && isAlive(board, i, j + 1)) {
54+
result += 1;
55+
}
56+
if (i + 1 < board.length && j - 1 >= 0 && isAlive(board, i + 1, j - 1)) {
57+
result += 1;
58+
}
59+
if (i + 1 < board.length && isAlive(board, i + 1, j)) {
60+
result += 1;
61+
}
62+
if (i + 1 < board.length && j + 1 < board[i].length && isAlive(board, i + 1, j + 1)) {
63+
result += 1;
64+
}
65+
return result;
66+
}
67+
}

0 commit comments

Comments
 (0)