V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  shawnyin  ›  全部回复第 1 页 / 共 1 页
回复总数  2
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   ·   实用小工具   ·   2986 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 14:04 · PVG 22:04 · LAX 06:04 · JFK 09:04
Developed with CodeLauncher
♥ Do have faith in what you're doing.