Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4f275a5

Browse files
game of life
1 parent69779a7 commit4f275a5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎MEDIUM/src/medium/GameOfLife.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagemedium;
2+
3+
publicclassGameOfLife {
4+
publicvoidgameOfLife(int[][]board) {
5+
intheight =board.length,width =board[0].length;
6+
int[][]next =newint[height][width];
7+
8+
for(inti =0;i <board.length;i++){
9+
for(intj =0;j <board[0].length;j++){
10+
intliveCellsCount =0;
11+
//count all its live cells
12+
13+
if(j+1 <width &&board[i][j+1] ==1)liveCellsCount++;//right cell
14+
if(j-1 >=0 &&board[i][j-1] ==1)liveCellsCount++;//left cell
15+
if(i+1 <height &&board[i+1][j] ==1)liveCellsCount++;//down cell
16+
if(i-1 >=0 &&board[i-1][j] ==1)liveCellsCount++;//up cell
17+
if(i-1 >=0 &&j-1 >=0 &&board[i-1][j-1] ==1)liveCellsCount++;//up left cell
18+
if(i-1 >=0 &&j+1 <width &&board[i-1][j+1] ==1)liveCellsCount++;//up right cell
19+
if(i+1 <height &&j-1 >=0 &&board[i+1][j-1] ==1)liveCellsCount++;//down left cell
20+
if(i+1 <height &&j+1 <width &&board[i+1][j+1] ==1)liveCellsCount++;//down right cell
21+
22+
if(board[i][j] ==1){
23+
if(liveCellsCount >3 ||liveCellsCount <2) {
24+
next[i][j] =0;
25+
}else {
26+
next[i][j] =1;
27+
}
28+
}elseif(board[i][j] ==0) {
29+
if(liveCellsCount ==3){
30+
next[i][j] =1;
31+
}
32+
}
33+
}
34+
}
35+
36+
for(inti =0;i <board.length;i++){
37+
for(intj =0;j <board[0].length;j++){
38+
board[i][j] =next[i][j];
39+
}
40+
}
41+
}
42+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution]| ? | ? | Hard| BFS
2323
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../blob/master/EASY/src/easy/BullsandCows.java)| O(n)|O(1)| Easy|
2424
|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../../blob/master/EASY/src/easy/NimGame.java)| O(1)|O(1)| Easy|
25+
|289|[Game of Life](https://leetcode.com/problems/game-of-life/)|[Solution](../../blob/master/MEDIUM/src/medium/GameOfLife.java)| O(m*n)|O(m*n), could be optimized to O(1)| Medium|
2526
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../../blob/master/EASY/src/easy/MoveZeroes.java)| O(n)|O(1)| Easy|
2627
|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[Solution](../../blob/master/MEDIUM/src/medium/WiggleSort.java)| O(n)|O(1)| Medium|
2728
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)|[Solution](../../blob/master/EASY/src/easy/FirstBadVersion.java)| O(logn)|O(1) | Easy| Binary Search

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp