1. 题目

2. 分析

这道题比较典型,跟最小路径和 是同样的思想。比较简单。

3. 代码

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        row = len(obstacleGrid)
        col = len(obstacleGrid[-1])        
        dp = [[0] *(col) for i in range(row)]
        
        if obstacleGrid[0][0] == 1:
            return 0
        dp[0][0] = 1
        print(dp)
        for i in range(row):
            for j in range(col):
                if obstacleGrid[i][j]: # 当前是障碍物                    
                    continue
                if i==0 and j ==0:
                    continue
                if 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] 
        print(dp)
        return dp[row-1][col-1]