赞
踩
62、不同路径:
- class Solution(object):
- def uniquePaths(self, m, n):
- """
- :type m: int
- :type n: int
- :rtype: int
- """
- dp = [[0]*n for _ in range(m)]
- for i in range(m):
- dp[i][0] = 1
- for j in range(n):
- dp[0][j] = 1
- for i in range(1, m):
- for j in range(1, n):
- dp[i][j] = dp[i-1][j] + dp[i][j-1]
- return dp[m-1][n-1]
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
63、不同路径II:
- class Solution(object):
- def uniquePathsWithObstacles(self, obstacleGrid):
- """
- :type obstacleGrid: List[List[int]]
- :rtype: int
- """
- m = len(obstacleGrid)
- n = len(obstacleGrid[0])
- if obstacleGrid[0][0] == 1:
- return 0
- dp = [[0]*n for _ in range(m)]
- dp[0][0] = 1
- for i in range(m):
- for j in range(n):
- if i == j == 0:
- continue
- elif obstacleGrid[i][j] == 1:
- dp[i][j] = 0
- elif j == 0 and i > 0:
- dp[i][j] = dp[i-1][j]
- elif i == 0 and j > 0:
- dp[i][j] = dp[i][j-1]
- else:
- dp[i][j] = dp[i-1][j] + dp[i][j-1]
- return dp[m-1][n-1]
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。