LeetCode 529.Minesweeper & 515.Find Largest Value in Each Tree Row

Problem529 Minesweeper

You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.

Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:

  1. If a mine ('M') is revealed, then the game is over - change it to 'X'.
  2. If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
  3. If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
  4. Return the board when no more squares will be revealed.

Example 1:

Input: 

[['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'M', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E'],
 ['E', 'E', 'E', 'E', 'E']]

Click : [3,0]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Example 2:

Input: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'M', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Click : [1,2]

Output: 

[['B', '1', 'E', '1', 'B'],
 ['B', '1', 'X', '1', 'B'],
 ['B', '1', '1', '1', 'B'],
 ['B', 'B', 'B', 'B', 'B']]

Explanation:

Note:

  1. The range of the input matrix's height and width is [1,50].
  2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  3. The input board won't be a stage when game is over (some mines have been revealed).
  4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares


解题思路:

1. 深度搜索(DFS),从点击的点开始,如果点击的点是炸弹M,则gameover,如果不是炸弹,则记录它周围的炸弹的数量,如果周围没有炸弹,则将值设为'B',并重新开始搜索该点的邻接点,如果周围有炸弹,则将值设为炸弹的数量。

2. 在判定周围炸弹情况的时候要考虑该点在矩阵中的位置,防止数组越界

3. 要考虑递归结束的条件,已经搜索过的点不需要再继续搜索。


代码如下:

class Solution {
  private char[][] gameBoard;
  private boolean[][] isRevealed;
    public char[][] updateBoard(char[][] board, int[] click) {
      gameBoard = board;
      
      if(isGameOver(click)){
        return gameBoard;
      }
      else{
        reveal(click);
      }
      return gameBoard;
        
    }


    public void reveal(int[] click){

      int x = click[0];
      int y = click[1];
      int count = 0;      
      
      if(gameBoard[x][y] == 'M'){
        return;
      }
      for(int i = x-1 >= 0? x-1 : x;i<gameBoard.length && i < x+2;i++){
          for(int j = y-1 >= 0? y-1 : y;j < gameBoard[i].length && j<y+2;j++){
            if(gameBoard[i][j] == 'M'){
              count++;
           }          
         }
      }
      if(count == 0){
        gameBoard[x][y] = 'B';
        for(int i = x-1 >= 0? x-1 : x;i<gameBoard.length && i < x+2;i++){
          for(int j = y-1 >= 0? y-1 : y;j < gameBoard[i].length && j<y+2;j++){
            if(gameBoard[i][j] == 'B'){
              continue;
            }
            else{
              int[] temp = {i,j};
              reveal(temp);
           }

          }
        }

      }
      if(count > 0){
        gameBoard[x][y] = (char) (count+48);
      }
      return;


    }

    public boolean isGameOver(int[] click){
      if(gameBoard[click[0]][click[1]] == 'M'){
        gameBoard[click[0]][click[1]] = 'X';
        return true;
      }
      else{
        return false;
      }
    }
}


Problem515 Find Largest Value in Each Tree Row

You need to find the largest value in each row of a binary tree.

Example:

Input: 

          1
         / \
        3   2
       / \   \  
      5   3   9 

Output: [1, 3, 9]

解题思路:

1. 利用深度搜索遍历二叉树,记录层数,每遍历一个节点,对他的左右两个子节点的值进行比较,找出最大值,作为该层的最大值候补。

2. 利用一个HashMap<Interger,Interger>来保存每一层的最大值,前面key表示层数,后面的value表示该层的最大值。

3.每层的最大候补跟HashMap中该层的最大值比较,取最大值。如果HashMap中没有则该层的最大值,则直接插入。


代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private Map<Integer,Integer> Max;
    
    public List<Integer> largestValues(TreeNode root) {
      List<Integer> valueList = new ArrayList<>();
      Max = new HashMap<>();
      if(root == null) return valueList;
      Max.put(0,root.val);
      findMax(root,0);
      int i = 0;
      while(Max.containsKey(i)){
        valueList.add(Max.get(i));
        i++;
      }
      return valueList;
      
        
    }

    public void findMax(TreeNode root,int floor){
      if(root ==null ||(root.left == null && root.right == null)){
        return;
      }

      int temp,leftVal,rightVal;
      if(root.left == null){
        rightVal = root.right.val;
        temp = rightVal;        
        }
      else if(root.right == null) {
        leftVal = root.left.val;
        temp = leftVal;
      }
      else{
        leftVal = root.left.val;
        rightVal = root.right.val;
        temp =  leftVal>rightVal? leftVal:rightVal;
      }
      
      floor++;
      if(Max.containsKey(floor)){
        if(temp > Max.get(floor)){
          Max.put(floor,temp);
        }
      }
      else{
        Max.put(floor,temp);
      }
      
    findMax(root.left,floor);
     
    findMax(root.right,floor);
      
      return;

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值