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];
        }
    }
  • 相关阅读:
    jquery异步加载json格式的数据
    三角形及选中取消按钮的css代码
    css实现自适应宽度布局
    table表格中实现tbody部分可滚动,且thead部分固定
    table数据表格添加checkbox进行数据进行两个表格左右移动。
    对checkbox 的checked的一些总结
    Java多线程同步器
    Springboot动态获取bean对象工具类
    并发阻塞队列和非阻塞队列详解
    多线程-volatile关键字和ThreadLocal详解
  • 原文地址:https://www.cnblogs.com/silentteller/p/11657316.html
Copyright © 2011-2022 走看看