-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameoflife.cpp
More file actions
33 lines (32 loc) · 865 Bytes
/
gameoflife.cpp
File metadata and controls
33 lines (32 loc) · 865 Bytes
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
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size();
int n = m ? board[0].size() : 0;
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
int count = 0;
for(int I=max(i-1,0);I<min(i+2,m);I++)
{
for(int J=max(j-1,0);J<min(j+2,n);J++)
{
count += board[I][J] & 1;
}
}
if(count==3||count-board[i][j]==3)
{
board[i][j] |= 2;
}
}
}
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
board[i][j] = board[i][j] >> 1;
}
}
}
};