2 Star 0 Fork 0

xinanXu/myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC289.cpp 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
xinanXu 提交于 2023-02-17 00:42 . 289 生命游戏
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
if (board.size() == 0) return;
int boardi = board.size(), boardj = board[0].size();
vector<vector<int>> ret(boardi, vector<int>(boardj));
for (int i = 0; i < boardi; i++) {
for (int j = 0; j < boardj; j++) {
int ali = 0, now = board[i][j];
for (int ii = i - 1; ii <= i + 1; ii++) {
if (ii < 0 || ii >= boardi) continue;
for (int jj = j - 1; jj <= j + 1; jj++) {
if (jj < 0 || jj >= boardj) continue;
if (board[ii][jj]) ali += 1;
}
}
ali -= now;
if (now && ali < 2) now = 0;
if (now && (ali == 2 || ali == 3)) now = 1;
if (now && ali > 3) now = 0;
if (!now && ali == 3) now = 1;
ret[i][j] = now;
}
}
board = ret;
}
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/DearAtri/myleetcode.git
git@gitee.com:DearAtri/myleetcode.git
DearAtri
myleetcode
myleetcode
master

搜索帮助