算法刷题Day29:BM67 不同路径的数目(一)
题目链接
描述
解题思路:
- 二维dp数组初始化。
dp[i][0] = 1
,dp[0][j] = 1
。因为到达第一行第一列的每个格子只能有一条路。 - 状态转移
dp[i][j] = dp[i-1][j] + dp[i][j-1]
代码:
class Solution:
def uniquePaths(self , m: int, n: int) -> int:
# write code here
dp = [[0 for _ in range(n)] for _ in range(m)]
# init
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[-1][-1]