Leetcode 289 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.
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?
思路:要求 in-place ,所以要区分三种变化状态 1->0,1->1,0->1,将三种状态分别用-1/-2/-3三个状态值来表示
第一次遍历时,先判断当前cell的转换状态,且将cell值置为对应状态值,在后面的遍历中根据状态值则可推断出原始值,进而判断转换状态。
第二次遍历,则将状态值置为变换后的值,注意一下在每个cell遍历其周围八个点时用的循环,是怎么排除边界情况的
public class S289 {
public void gameOfLife(int[][] board) {
int m = board.length,n = board[0].length;
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
int liveCount = 0;
for (int k = i-1;k <= i+1;k++) {
for (int l = j-1;l <= j+1;l++) {
if(k < 0 || l < 0 || k > m-1 || l > n-1 || (k == i && l == j)) continue;
liveCount += getRealNum(board[k][l]);
}
}
if (board[i][j] == 1) {
if (liveCount <2 || liveCount >3 ) {
board[i][j] = -1;
} else {
board[i][j] = -2;
}
} else {
if (liveCount == 3) {
board[i][j] = -3;
}
}
}
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
if (board[i][j] == -2 || board[i][j] == -3)
board[i][j] = 1;
else if (board[i][j] == -1)
board[i][j] = 0;
}
}
}
public static int getRealNum(int i){
if(i == -1 || i == -2)
return 1;
else if (i == -3) {
return 0;
}
return i;
}
public static void main(String[] args) {
S289 s = new S289();
int [][]b = {{1}};
s.gameOfLife(b);
}
}
Leetcode 289 Game of Life的更多相关文章
- leetcode@ [289] Game of Life (Array)
https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- LeetCode 289. Game of Life (生命游戏)
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- LeetCode 289. Game of Life (C++)
题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- leetcode 289生命游戏
class Solution { public: vector<vector<,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; void gameOfLife( ...
- LeetCode | 289. 生命游戏(原地算法/位运算)
记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...
- 2017-3-9 leetcode 283 287 289
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
随机推荐
- 利用VR技术,走进白宫,走进奥巴马
奥马巴卸任演讲由诺基亚OZO全程360° VR直播,并不是每个人都能去白宫拜访,一窥白宫内部的模样,更不可能有多少人有机会在奥马巴面前听他讲讲他的家. 现在前任总统巴拉克奥巴马以及其夫人米歇尔奥巴 ...
- vultr新用户注册享受50美元优惠码,长期有效
vultr vps服务器,我用了三年多,购买了几十台vps,性价比非常高. 近期,vutlr推出了最新优惠码DOMORE长期有效,新用户注册账号时候,可在付款方式界面输入这个优惠码,享受50美元余额, ...
- php 中数据类型
总体划分 8 中 1基本类型(标量) 整型 int 整型的三种写法 <?php $n1 = 123; //10进制 $n2 = 0123; //8进制 $n3 = 0x123;//16进制 // ...
- spring、struts整合
package com.hanqi.test; public class JISQ { public double add(double a,double b) { return (a+b); } } ...
- [HMLY]6.iOS Xcode全面剖析
一.创建一个新工程 1.第一步打开Xcode,找到Xcode程序图标并点击 2.如下界面,我们点击新建一个项目,即第二项 (1).Get started with a playground playg ...
- 使用 Jenkins 搭建 iOS/Android 持续集成打包平台【转】
背景描述 根据项目需求,现要在团队内部搭建一个统一的打包平台,实现对iOS和Android项目的打包.而且为了方便团队内部的测试包分发,希望在打包完成后能生成一个二维码,体验用户(产品.运营.测试等人 ...
- CodeForces 546D
Description 两个士兵在玩一个游戏,开始的时候第一个士兵选择一个数n,并把这个数交给第二个士兵,第二个士兵必须选择一个x满足x>1 且n能被x整除,然后将n变为n/x,然后把这个数交给 ...
- spring HttpServletRequest 简介
前提:工作遇到controller中通过注解的方式注入 @Resourceprivate HttpServletRequest request;我们都知道spring 默认是单例,当遇到并发的时候线程 ...
- JavaFx的一些总结
JavaFX的一些总结:1.场景scene.布局layout.控件control是javaFX的基本组成部分.控件可以放到布局中,布局可以放到布局中,也可以放到场景中,这里要注意,场景里只能放一个布 ...
- Linux c 内存高速访问
概述 要想高速利用内存就必须高效利用cpu cache,关于cpu cache这里就不多加讨论了,自己感兴趣可以google 而cpu访问内存的单位是cache line,因此高效利用cache li ...