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
public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[][] dp = new int[m][n]; int i= 0; while(i<m){ if(obstacleGrid[i][0] == 1){ dp[i++][0] = 0; while(i<m){ dp[i++][0] = 0; } } if(i<m) { dp[i++][0] = 1; } } int j = 0; while(j<n){ if(obstacleGrid[0][j] == 1){ dp[0][j++] = 0; while(j<n){ dp[0][j++] = 0; } } if(j<n) { dp[0][j++] = 1; } } for(int i1 = 1;i1<m;i1++){ for(int j1 = 1;j1<n;j1++){ if(obstacleGrid[i1][j1] == 1){ dp[i1][j1] = 0; }else { dp[i1][j1] = dp[i1 - 1][j1] + dp[i1][j1 - 1]; } } } return dp[m-1][n-1]; }
没什么难度。
——2020.6.28