【刷题-LeetCode】289. Game of Life
- Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
- Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population..
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[
[0,0,0],
[1,0,1],
[0,1,1],
[0,1,0]
]
Follow up:
- Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
解法1 将原矩阵复制下来,按照游戏规则修改原来的矩阵
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
vector<vector<int>>tmp_board(board.begin(), board.end());
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
int cnt = 0;
for(int k = 0; k < 8; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) && tmp_board[tmp_x][tmp_y]){
cnt++;
}
}
if(tmp_board[i][j] == 1){
if(cnt < 2 || cnt > 3){
board[i][j] = 0;
}else{
board[i][j] = 1;
}
}else{
if(cnt == 3)board[i][j] = 1;
else board[i][j] = 0;
}
}
}
}
private:
int dx[8] = {-1, 0, -1, -1, 0, 1, 1, 1};
int dy[8] = {0, -1, -1, 1, 1, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
};
解法2 原地修改,\(O(1)\)空间复杂度。使用多个状态:
- 0:原来是0,新的还是0
- 1:原来是1,新的还是1
- 2:原来是0,新的是1
- 3:原来是1,新的是0
按照行顺序更新时,对于每个cell,左、上、左上、右上是被更新了,剩下四个没有更新,按照对应的数值统计出在原始矩阵中的数字,然后更新当前cell,最后遍历一遍,把2和3分别修改成1和0
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
int cnt = 0;
for(int k = 0; k < 4; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) &&
(board[tmp_x][tmp_y] == 3 || board[tmp_x][tmp_y] == 1)){
cnt++;
}
}
for(int k = 4; k < 8; ++k){
int tmp_x = i + dx[k], tmp_y = j + dy[k];
if(valid(tmp_x, tmp_y, m, n) && board[tmp_x][tmp_y] == 1){
cnt++;
}
}
if(board[i][j] == 1){
if(cnt < 2 || cnt > 3){
board[i][j] = 3;
}else{
board[i][j] = 1;
}
}else{
if(cnt == 3)board[i][j] = 2;
else board[i][j] = 0;
}
}
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(board[i][j] == 2)board[i][j] = 1;
else if(board[i][j] == 3)board[i][j] = 0;
}
}
}
private:
int dx[8] = {-1, 0, -1, -1, 0, 1, 1, 1};
int dy[8] = {0, -1, -1, 1, 1, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
};
【刷题-LeetCode】289. Game of Life的更多相关文章
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- [刷题] Leetcode算法 (2020-2-27)
1.最后一个单词的长度(很简单) 题目: 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在 ...
- bash 刷题leetcode
题目一: 给定一个文本文件 file.txt,请只打印这个文件中的第十行. 示例: 假设 file.txt 有如下内容: Line 1 Line 2 Line 3 Line 4 Line 5 Line ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- 【刷题-LeetCode】306. Additive Number
Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...
- 【刷题-LeetCode】304. Range Sum Query 2D - Immutable
Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【刷题-LeetCode】275. H-Index II
H-Index II Given an array of citations sorted in ascending order (each citation is a non-negative in ...
随机推荐
- CF53C Little Frog 题解
Content 有一只小青蛙想游历 \(n\) 块土堆,它现在在 \(1\) 号土堆上,每次可以跳跃任意距离到达另外的一个土堆.它想让每次跳跃的距离都不相等,试找到这样的一个方案. 数据范围:\(1\ ...
- 【嵌入式AI】全志 XR806 OpenHarmony 鸿蒙系统固件烧录
欢迎关注我的公众号 [极智视界],回复001获取Google编程规范 O_o >_< o_O O_o ~_~ o_O 大家好,我是极智视界,本教程详细记录了 ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- C. The Meaningless Game
C. The Meaningless Game 题目链接 题意 给你两个数,开始都为1,然后每轮可以任选一个k,一边可以乘以\(k\),另一边乘以\(k^2\),然后问你最终是否可以得到所给的两个数a ...
- 快速登陆linux服务器
前言 本文适用于喜欢原生终端的用户,钟爱第三方ssh客户端的可以无视....客户端可以保存用户信息和密码,比较无脑.mac可以使用终端,win可以使用git的bash. 上次分享了配置非对称秘钥免密登 ...
- 第十六个知识点:描述DSA,Schnorr,RSA-FDH的密钥生成,签名和验证
第十六个知识点:描述DSA,Schnorr,RSA-FDH的密钥生成,签名和验证 这是密码学52件事系列中第16篇,这周我们描述关于DSA,Schnorr和RSA-FDH的密钥生成,签名和验证. 1. ...
- Decoupling Representation and Classifier for Long-tailed Recognition
目录 概 主要内容 Sampling 分类器 代码 Kang B., Xie S., Rohrbach M., Yan Z., Gordo A., Feng J. and Kalantidis Y. ...
- 排列组合 "n个球放入m个盒子m"问题 总结
求,盒子都可以分成是否不能区分,和能区分,还能分成是否能有空箱子,所以一共是8种情况,我们现在来一一讨论. 1.球同,盒不同,无空箱 C(n-1,m-1), n>=m0, n<m 使用插板 ...
- Java网络编程Demo,使用TCP 实现简单群聊功能GroupchatSimple,多个客户端输入消息,显示在服务端的控制台
效果: 服务端 客户端 实现代码: 服务端 import java.io.IOException; import java.net.ServerSocket; import java.net.Sock ...