class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
DP:
状态转移方程:P(i,j)=P(i-1,j)+P(i,j-1)
"""
dp=[[0]*m for i in range(n)]#dp二维数组,只保存当前格走法数
dp[0][0]=1
for i in range(n):
for j in range(m):
if i==0 and j==0:
pass
elif j==0:#上顶格
dp[i][j]=dp[i-1][j]
elif i==0:#左顶格
dp[i][j]=dp[i][j-1]
else:
dp[i][j]=dp[i-1][j]+dp[i][j-1]
return dp[-1][-1]
a=Solution()
print a.uniquePaths(3,7)
62不同路径 动态规划(DP)
最新推荐文章于 2023-09-04 17:08:01 发布