LeetCode.289 - Game of Life
阿新 • • 發佈:2020-08-22
My solution:
class Solution { vector<pair<int, int>> points{ {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} }; public: void gameOfLife(vector<vector<int>>& board) { vector<vector<int>> copy = board; for (int i = 0; i < copy.size(); i++) { for (int j = 0; j < copy[0].size(); j++) { int count = 0; for (auto p : points) { int x = i + p.first, y = j + p.second; if (x < 0 || x >= copy.size() || y < 0 || y >= copy[0].size()) continue; count += copy[x][y]; } if (copy[i][j]) { if (count < 2) board[i][j] = 0; else if (count > 3) board[i][j] = 0; } else { if (count == 3) board[i][j] = 1; } } } } };