码迷,mamicode.com
首页 > 其他好文 > 详细

73. Set Matrix Zeroes && 289. Game of Life

时间:2016-07-25 08:09:02      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

73. Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Hide Tags
 Array
Hide Similar Problems
 (M) Game of Life
 
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;
  }
}

 

289. Game of Life

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):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.

Follow up: 

  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
 
Hide Tags
 Array
Hide Similar Problems
 (M) Set Matrix Zeroes
 
Referenced https://discuss.leetcode.com/topic/29054/easiest-java-solution-with-explanation
 
/**
 * [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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!