62. 不同路径
解法一:计算组合数
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
res = 1
total = m-1 + n-1
#计算组合数
for i in range(total, m-1, -1):
res = res * i #分子累乘
for j in range(1, n):
res = res // j #分母累除
return res解法二:dp
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
path = [[1]*m for _ in range(n)] #记录表
for i in range(1, n): #从第1行开始
for j in range(1, m): #从第1列开始
path[i][j] = path[i-1][j] + path[i][j-1]
return path[n-1][m-1]最后更新于