[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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]; } }; |
类似题目:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步