LeetCode(力扣)63. 不同路径 IIPython

LeetCode63. 不同路径 II

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/unique-paths-ii/
LeetCode(力扣)63. 不同路径 IIPython_第1张图片

代码

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        if obstacleGrid[m - 1][n - 1] == 1 or obstacleGrid[0][0] == 1:
            return 0
        
        bq = [[0] * n for _ in range(m)]

        for i in range(m):
            if obstacleGrid[i][0] == 0:
                bq[i][0] = 1
            else:
                break
        for i in range(n):
            if obstacleGrid[0][i] == 0:
                bq[0][i] = 1
            else:
                break
        
        for i in range(1, m):
            for j in range(1, n):
                if obstacleGrid[i][j] == 1:
                    continue
                else:
                    bq[i][j] = bq[i - 1][j] + bq[i][j - 1]
        return bq[m - 1][n - 1]

你可能感兴趣的:(leetcode,算法,职场和发展,python)