class Solution:
"""
@param obstacleGrid: An list of lists of integers
@return: An integer
"""
def uniquePathsWithObstacles(self, obstacleGrid):
# write your code here
if len(obstacleGrid) == 0:
return 0
m = len(obstacleGrid)
n = len(obstacleGrid[0])
ans = [[1 for i in range(n)] for i in range(m)]
i_index = n
j_index = m
for i in range(n):
if obstacleGrid[0][i] == 1:
i_index = i
break
for i in range(i_index, n):
ans[0][i] = 0
for j in range(m):
if obstacleGrid[j][0] == 1:
j_index = j
break
for j in range(j_index, m):
ans[j][0] = 0
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 1:
ans[i][j] = 0
else:
ans[i][j] = ans[i-1][j] + ans[i][j-1]
return ans[m-1][n-1]
LintCode:不同的路径II
最新推荐文章于 2019-05-25 02:21:17 发布