Description
1 | According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." |
This problem is kind of easy if we deal with it by O(n n) time complexity and O(n n) space complexity, however, according to the follow up, we should come up with a O(1) space solution.
Obviously, we have to modify the original matrix directly to get O(1) space, but if we change the cell before, we can not get correct number of live neighbors after that.
Therefore, we can use bit manipulation to handle it. The code is very easy to understand - we can use 2 bits to store the information, the 1st bit is whether it is live after 1st round, the 2nd bit is whether it is live before 1st round.
1 | class Solution: |