289. 生命游戏
代码
javascript
// 289. 生命游戏:https://leetcode.cn/problems/game-of-life/
// 输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
// 输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
export function gameOfLife (board) {
const m = board.length
const n = board[0].length
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const count = countLiveNeighbors(board, i, j)
if (board[i][j] === 1) {
if (count < 2 || count > 3) {
board[i][j] = -1
}
}
if (board[i][j] === 0) {
if (count === 3) {
board[i][j] = 2
}
}
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === -1) {
board[i][j] = 0
} else if (board[i][j] === 2) {
board[i][j] = 1
}
}
}
function countLiveNeighbors (board, i, j) {
let count = 0
for (let x = i - 1; x <= i + 1; x++) {
for (let y = j - 1; y <= j + 1; y++) {
if (x === i && y === j) continue
if (board[x]?.[y] === 1 || board[x]?.[y] === -1) count++
}
}
return count
}
}
typescript
// 289. 生命游戏:https://leetcode.cn/problems/game-of-life/
// 输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
// 输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
export function gameOfLife (board: number[][]): void {
const m = board.length
const n = board[0].length
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const count = countLiveNeighbors(board, i, j)
if (board[i][j] === 1) {
if (count < 2 || count > 3) {
board[i][j] = -1
}
}
if (board[i][j] === 0) {
if (count === 3) {
board[i][j] = 2
}
}
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === -1) {
board[i][j] = 0
} else if (board[i][j] === 2) {
board[i][j] = 1
}
}
}
function countLiveNeighbors (board: number[][], i: number, j: number): number {
let count = 0
for (let x = i - 1; x <= i + 1; x++) {
for (let y = j - 1; y <= j + 1; y++) {
if (x === i && y === j) continue
if (board[x]?.[y] === 1 || board[x]?.[y] === -1) count++
}
}
return count
}
}
测试代码
ts
import { expect, test } from 'vitest'
import { gameOfLife } from './typescript.ts'
import { gameOfLife as gameOfLifeJs } from './javascript.js'
test(`gameOfLife`, () => {
const board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
gameOfLife(board)
expect(board).toEqual([[0,0,0],[1,0,1],[0,1,1],[0,1,0]])
const board1 = [[1,1],[1,0]]
gameOfLife(board1)
expect(board1).toEqual([[1,1],[1,1]])
})
test(`gameOfLifeJs`, () => {
const board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
gameOfLifeJs(board)
expect(board).toEqual([[0,0,0],[1,0,1],[0,1,1],[0,1,0]])
const board1 = [[1,1],[1,0]]
gameOfLifeJs(board1)
expect(board1).toEqual([[1,1],[1,1]])
})