leetcode——63. 不同路径 II

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid) -> int:
        if len(obstacleGrid)<1:
            return 0
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        if m==1 and n==1:
            if obstacleGrid[0][0]==0:
                return 1
            else:
                return 0
        memo=[[0 for i in range(n)] for i in range(m)]#定义空二维数组
        for i in range(n):
            if obstacleGrid[0][i]==1:
                memo[0][i]=0
                for k in range(i+1,n):
                    memo[0][k]=0
                break
            else:
                memo[0][i]=1
        for j in range(m):
            if obstacleGrid[j][0]==1:
                memo[j:][0]=[0]*(m-j-1)
                break
            else:
                memo[j][0]=1
        for i in range(1,m):
            for j in range(1,n):
                if obstacleGrid[i][j]==1:
                    memo[i][j]=0
                else:
                    memo[i][j]=memo[i-1][j]+memo[i][j-1]
        return memo[m-1][n-1]
执行用时 :64 ms, 在所有 python3 提交中击败了76.80%的用户 内存消耗 :13.7 MB, 在所有 python3 提交中击败了5.19%的用户                                                     ——2019.10.14    
上一篇:第4章-人工智能


下一篇:63不同路径II