-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path289.cpp
More file actions
24 lines (24 loc) · 787 Bytes
/
289.cpp
File metadata and controls
24 lines (24 loc) · 787 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
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
vector<int> dirs = {-1,-1,0,1,1,1,0,-1,-1,-1};
int r = board.size(), c = board[0].size();
vector<vector<int>> res = board;
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
int live = 0;
for(int k = 0; k<8; k++){
int nr = i+dirs[k];
int nc = j+dirs[k+2];
if(0<=nr && 0<=nc && nr<r && nc<c && res[nr][nc]==1)
live++;
}
if(res[i][j]==1 && live<2 || live>3)
board[i][j] = 0;
if(res[i][j]==0 && live==3)
board[i][j] = 1;
}
}
return;
}
};