标签:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
public class Solution { //Only consider the zeros that exist originally. public void setZeroes(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; //Algorithm: use the first row and first column to save original zeros info //first check first row to see any zeros boolean clearFirstRow = false; for (int c = 0; c < col; ++c) if (matrix[0][c] == 0) { clearFirstRow = true; break; } boolean clearFirstColumn = false; for (int r = 0; r < row; ++r) if (matrix[r][0] == 0) { clearFirstColumn = true; break; } //mark the rows and columns to clear for (int r = 1; r < row; ++r) { for (int c = 1; c < col; ++c) { if (matrix[r][c] == 0) { matrix[0][c] = 0; matrix[r][0] = 0; } } } //clear the inner matrix to zeroes for (int r = 1; r < row; ++r) { for (int c = 1; c < col; ++c) { if (matrix[0][c] == 0 || matrix[r][0] == 0) { matrix[r][c] = 0; } } } if (clearFirstRow) for (int c = 0; c < col; ++c) matrix[0][c] = 0; if (clearFirstColumn) for (int r = 0; r < row; ++r) matrix[r][0] = 0; } }
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."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with itseight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
Write a function to compute the next state (after one update) of the board given its current state.
Follow up:
/** * [2nd bit, 1st bit] = [next state(dead by default), current state] * - 00 dead (next) <- dead (current) * - 01 dead (next) <- live (current) * - 10 live (next) <- dead (current) * - 11 live (next) <- live (current) * <p> * To get the current state: board[i][j] & 1 * To get the next state: board[i][j] >> 1 * <p> * Transitions: * Under population: 01 -> 01: this == 1 and neighbor < 2 (no change, no need to consider) * Live on: 01 -> 11: this == 1 and neighbor ==2 or ==3 * Over population: 01 -> 01: this == 1 and neighbor > 3 (no change, no need to consider) * Reproduction: 00 -> 10: this == 0 and neighbor ==3 */ public class Solution { //This game assumes all cells instantly change from the 1st to the 2nd state at the same time. public void gameOfLife(int[][] board) { if (board == null || board.length == 0) return; int ROW = board.length; int COL = board[0].length; for (int r = 0; r < ROW; ++r) { for (int c = 0; c < COL; ++c) { int lives = liveNeighbors(board, ROW, COL, r, c); //Transition: Live on if (board[r][c] == 1 && (lives == 2 || lives == 3)) board[r][c] = 3; //Transition: Reproduction if (board[r][c] == 0 && lives == 3) board[r][c] = 2; } } //Remove every cell‘s 1st state for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) board[i][j] >>= 1; // Get the 2nd state } //return the number of live neighbors private int liveNeighbors(int[][] board, int ROW, int COL, int cR, int cC) { int lives = 0; for (int r = Math.max(cR - 1, 0); r <= Math.min(cR + 1, ROW - 1); ++r) for (int c = Math.max(cC - 1, 0); c <= Math.min(cC + 1, COL - 1); ++c) lives += board[r][c] & 1; lives -= board[cR][cC] & 1; //cannot consider itself. return lives; } }
73. Set Matrix Zeroes && 289. Game of Life
标签:
原文地址:http://www.cnblogs.com/neweracoding/p/5702206.html