Medium
这题和62. 不同路径基本是一样的,唯一增加的条件就是中间可能有障碍物的情况
dp
数组,dp[i][j]
表示(i,j)
节点可能的路径(i,j)
节点恰好是障碍物,可能路径为0
dp[i][j] = dp[i-1][j] + dp[i][j-1]
1
但是如果障碍物在边界,碰到之后,后面的都是0
以上,尝试写一下代码,AC!
python3
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
row = len(obstacleGrid)
col = len(obstacleGrid[0])
dp = [[ 0 for _ in range(col) ] for _ in range(row)]
for k in range(row):
if obstacleGrid[k][0] == 1:
break
dp[k][0] = 1
for k in range(col):
if obstacleGrid[0][k] == 1:
break
dp[0][k] = 1
for i in range(1,row):
for j in range(1,col):
if obstacleGrid[i][j] == 1:
dp[i][j] = 0
else:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
print(dp)
return dp[-1][-1]