-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path289.java
More file actions
106 lines (92 loc) · 3.13 KB
/
289.java
File metadata and controls
106 lines (92 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public void gameOfLife(int[][] board) {
int M = board.length;
int N = board[0].length;
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
if(isLive(board,i,j)){
board[i][j] = board[i][j] | 2;
}
}
}
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
board[i][j] = board[i][j] / 2;
}
}
}
public boolean isLive(int[][] board, int row, int col){
int sum=0;
int M = board.length;
int N = board[0].length;
for(int i=row-1;i<=row+1;i++){
for(int j = col-1; j<=col+1; j++){
if(i==row && j==col){
continue;
}
if(i>=0 && i<M && j>=0 && j<N){
if((board[i][j] & 1)==1){
sum++;
}
}
}
}
if((board[row][col] & 1) == 1){
if(sum==2 || sum==3){
return true;
}
} else{
if(sum==3){
return true;
}
}
return false;
}
}
__________________________________________________________________________________________________
sample 34596 kb submission
class Solution {
int ZERO_TO_ONE = 2;
int ONE_TO_ZERO = 3;
public void gameOfLife(int[][] board) {
if (board == null || board.length == 0 ) return;
int n = board.length;
int m = board[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int c = getNeighbours(board, i, j);
if ((board[i][j] == 1 && (c == 2 || c == 3)) || (board[i][j] == 0 && c != 3)) continue;
if (board[i][j] == 1) {
board[i][j] = ONE_TO_ZERO;
} else {
board[i][j] = ZERO_TO_ONE;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == ONE_TO_ZERO) {
board[i][j] = 0;
} else if (board[i][j] == ZERO_TO_ONE) {
board[i][j] = 1;
}
}
}
}
public int getNeighbours(int[][] board, int x, int y) {
int n = board.length;
int m = board[0].length;
int res = 0;
for (int i = -1; i < 2; i++) {
if (x + i < 0 || x + i >= n) continue;
for (int j = -1; j < 2; j++) {
if (y + j < 0 || y + j >= m || (i == 0 && j == 0)) continue;
if (board[x + i][y + j] == 1 || board[x + i][y + j] == ONE_TO_ZERO) res++;
}
}
return res;
}
}
__________________________________________________________________________________________________