zoukankan      html  css  js  c++  java
  • LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目:

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

    The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

    Now consider if some obstacles are added to the grids. How many unique paths would there be?

    An obstacle and empty space is marked as 1 and 0 respectively in the grid.

    Note: m and n will be at most 100.

    Example 1:

    Input:
    [
      [0,0,0],
      [0,1,0],
      [0,0,0]
    ]
    Output: 2
    Explanation:
    There is one obstacle in the middle of the 3x3 grid above.
    There are two ways to reach the bottom-right corner:
    1. Right -> Right -> Down -> Down
    2. Down -> Down -> Right -> Right

    分析:

    和第62题思路类似,LeetCode 62. Unique Paths不同路径 (C++/Java)

    现在网格中有了障碍物,网格中的障碍物和空位置分别用1和 0来表示。

    无论是递推还是递归求解,只要加一个判断当前各自是否有障碍即可。在判断当前是否有解时,可以在最开始将二维数组全部赋值-1,如果求到子问题时不为-1,则可以直接返回已有的解,可以节省时间。

    注:1.起始位置可能有障碍物。

      2.还遇到一个问题: runtime error: signed integer overflow: 1053165744 + 1579748616 cannot be represented in type 'int' (solution.cpp),改成long即可。

    程序:

    C++

    //Solution 1
    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            int m = obstacleGrid.size();
            int n = obstacleGrid[0].size();
            vector<vector<long>> res(m+1, vector<long>(n+1, 0));
            for(int i = 1; i < m+1; ++i)
                for(int j = 1; j < n+1; ++j){
                    if(obstacleGrid[i-1][j-1] == 1){
                        res[i][j] = 0;
                    }
                    else if(i == 1 && j == 1){
                        res[i][j] = 1;
                    }
                    else{
                        res[i][j] = res[i-1][j] + res[i][j-1];
                    }
                }
            return res[m][n];
        }
    };
    //Solution 2
    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            int m = obstacleGrid.size();
            int n = obstacleGrid[0].size();
            res = vector<vector<int>>(m+1, vector<int>(n+1, -1));
            
            return solvePath(m, n, obstacleGrid);
        }
    private:
        vector<vector<int>> res;
        int solvePath(int m, int n, vector<vector<int>> &vec){
            if(m <= 0 || n <= 0) return 0;
            if(m == 1 && n == 1) return 1-vec[0][0];
            if(res[m][n] != -1) return res[m][n];
            if(vec[m-1][n-1] == 1){
                res[m][n] = 0;
            }
            else{
                res[m][n] = solvePath(m-1, n, vec) + solvePath(m, n-1, vec);
            }
            return res[m][n];
        }
    };

    Java

    class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m = obstacleGrid.length;
            int n = obstacleGrid[0].length;
            res = new int[m+1][n+1];
            for(int[] r : res) 
                Arrays.fill(r,-1);
            return solvePath(m, n, obstacleGrid);
        }
        private int[][] res;
        private int solvePath(int m, int n, int[][] o){
            if(m <= 0 || n <= 0) return 0;
            if(m == 1 && n == 1) return 1-o[0][0];
            if(res[m][n] != -1) return res[m][n];
            if(o[m-1][n-1] == 1){
                res[m][n] = 0;
            }
            else{
                res[m][n] = solvePath(m-1, n, o) + solvePath(m, n-1, o);
            }
            return res[m][n];
        }
    }
  • 相关阅读:
    84. Largest Rectangle in Histogram (Solution 2)
    84. Largest Rectangle in Histogram (Solution 1)
    73. Set Matrix Zeroes
    【JavaScript】Symbol 静态方法
    【JavaScript】Date
    【JavaScript】Math
    725. Split Linked List in Parts把链表分成长度不超过1的若干部分
    791. Custom Sort String字符串保持字母一样,位置可以变
    508. Most Frequent Subtree Sum 最频繁的子树和
    762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
  • 原文地址:https://www.cnblogs.com/silentteller/p/11657316.html
Copyright © 2011-2022 走看看