zoukankan      html  css  js  c++  java
  • [LeetCode] 63.Unique Paths II

    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. Unique Paths 的拓展, 有以下不同:

    1. 当(i, j)有障碍时dp[i][j] = 0
    2. dp[0][j]和dp[i][0]未必为1.
    dp[0][j] = obstacleGrid[0][j] ? 0 : dp[0][j-1]
    dp[i][0] = obstacleGrid[i][0] ? 0 : dp[i-1][0]
    3. 当obstacleGrid [0][0] = 1时,return 0
    解法:DP, 建立二维数组,dp[i][j]表示在某一位置能到达的不同路径数量,dp[i][j] = dp[i-1][j] + dp[i][j-1] ,如果某一位置grid[i][j]=1说明为障碍物,那么dp[i][j] = 0,还要注意i, j为0的情况。
     
    Java: 
    class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) { 
            int m = obstacleGrid.length; 
            int n = obstacleGrid[0].length; 
    
            if (m == 0 || n == 0) { 
                return 0;
            }
    
            if (obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1) {
                return 0;
            }
    
            int[][] dp = new int[m][n];
    
            dp[0][0] = 1; 
            for(int i = 1; i < n; i++){ 
                if(obstacleGrid[0][i] == 1) {
                    dp[0][i] = 0; 
                }
                else {
                    dp[0][i] = dp[0][i-1]; 
                }
            } 
    
            for(int i = 1; i < m; i++){ 
                if(obstacleGrid[i][0] == 1) {
                    dp[i][0] = 0; 
                }
                else {
                    dp[i][0] = dp[i-1][0]; 
                }
            } 
    
            for(int i = 1; i < m; i++){ 
                for(int j = 1; j < n; j++){ 
                    if(obstacleGrid[i][j] == 1) {
                        dp[i][j] = 0; 
                    }
                    else {
                        dp[i][j] = dp[i][j-1] + dp[i-1][j]; 
                    }
                } 
            } 
            return dp[m-1][n-1]; 
        }
    }

    Java:

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int width = obstacleGrid[0].length;
        int[] dp = new int[width];
        dp[0] = 1;
        for (int[] row : obstacleGrid) {
            for (int j = 0; j < width; j++) {
                if (row[j] == 1)
                    dp[j] = 0;
                else if (j > 0)
                    dp[j] += dp[j - 1];
            }
        }
        return dp[width - 1];
    }  

    Python:

    class Solution:
    
        def uniquePathsWithObstacles(self, obstacleGrid):
            mp = obstacleGrid
            for i in range(len(mp)):
                for j in range(len(mp[i])):
                    if i == 0 and j == 0:
                        mp[i][j] = 1 - mp[i][j]
                    elif i == 0:
                        if mp[i][j] == 1:
                            mp[i][j] = 0
                        else:
                            mp[i][j] = mp[i][j - 1]
                    elif j == 0:
                        if mp[i][j] == 1:
                            mp[i][j] = 0
                        else:
                            mp[i][j] = mp[i - 1][j]
                    else:
                        if mp[i][j] == 1:
                            mp[i][j] = 0
                        else:
                            mp[i][j] = mp[i - 1][j] + mp[i][j - 1]
            if mp[-1][-1] > 2147483647: 
                return -1
            else:
                return mp[-1][-1]
    

    Python: wo

    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            m, n = len(obstacleGrid), len(obstacleGrid[0])
            dp = [[0] * n for i in xrange(m)]
            for i in xrange(m):
                for j in xrange(n):
                    if obstacleGrid[i][j] == 1:
                        dp[i][j] == 0
                    else:
                        if i == 0 and j == 0:  # 写错成了 or 
                            dp[i][j] = 1
                        elif i == 0:
                            dp[i][j] = dp[i][j-1]
                        elif j == 0:
                            dp[i][j] = dp[i-1][j]                        
                        else:
                            dp[i][j] = dp[i-1][j] + dp[i][j-1]
                            
            return dp[-1][-1]    

    Python: wo

    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            m, n = len(obstacleGrid), len(obstacleGrid[0])
            dp = [[0] * n for i in xrange(m)]
            for i in xrange(m):
                for j in xrange(n):
                    if obstacleGrid[i][j] != 1:
                        if i == 0 and j == 0:
                            dp[i][j] = 1
                        elif i == 0:
                            dp[i][j] = dp[i][j-1]
                        elif j == 0:
                            dp[i][j] = dp[i-1][j]                        
                        else:
                            dp[i][j] = dp[i-1][j] + dp[i][j-1]
                            
            return dp[-1][-1]  

    C++:

    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
            int m = obstacleGrid.size() , n = obstacleGrid[0].size();
            vector<vector<int>> dp(m+1,vector<int>(n+1,0));
            dp[0][1] = 1;
            for(int i = 1 ; i <= m ; ++i)
                for(int j = 1 ; j <= n ; ++j)
                    if(!obstacleGrid[i-1][j-1])
                        dp[i][j] = dp[i-1][j]+dp[i][j-1];
            return dp[m][n];
        }
    };
    

      

     

    类似题目:

  • 相关阅读:
    Hive优化
    RDD
    从Hadoop MapReduce到Spark
    Spark on yarn模式
    Hive的web端配置——HWI
    Spark环境搭建
    java身份证号校验
    java手机号码、电子邮箱校验
    服务器运维的日常维护工作
    JavaSSM框架简介
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8469303.html
Copyright © 2011-2022 走看看