shawnyin 最近的时间轴更新
shawnyin

shawnyin

V2EX 第 359461 号会员,加入于 2018-10-29 19:17:21 +08:00
shawnyin 最近回复了
2020-01-13 09:01:14 +08:00
回复了 shawnyin 创建的主题 LeetCode LeetCode in Go, 我的 golang 刷题笔记 (Weekly Contest 同步更新)
@iDontEatCookie 一定要仔细阅读并理解群规哈,群规+群主联系方式 ( https://wisdompeak.github.io/lc-score-board/)
2019-07-18 13:53:07 +08:00
回复了 harley27 创建的主题 Java 初学 Java 写 生命游戏 失败,原因不明!
```java
class Solution {
public void gameOfLife(int[][] board) {
//use -1 to indicate the cell lives but die at this generation
//use 2 to indicate the cell dies but live at this generation
int[][] shift = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { 1, 1 }, { 1, -1 }, { -1, 1 },
{ -1, -1 } };
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int cnt_live = 0;
for (int k = 0; k < 8; k++) {
int x = i + shift[k][0];
int y = j + shift[k][1];
if (x >= 0 && y >= 0 && x < m && y < n && Math.abs(board[x][y]) == 1) {
cnt_live++;
}
}
if (board[i][j] == 0 && cnt_live == 3)
board[i][j] = 2;
if (board[i][j] == 1 && (cnt_live > 3 || cnt_live < 2)) {
board[i][j] = -1;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == -1)
board[i][j] = 0;
if (board[i][j] == 2)
board[i][j] = 1;
}
}
return;
}
}
```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1343 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 17:52 · PVG 01:52 · LAX 09:52 · JFK 12:52
Developed with CodeLauncher
♥ Do have faith in what you're doing.