题解 | #求路径#

求路径

http://www.nowcoder.com/practice/166eaff8439d4cd898e3ba933fbc6358

#
# 
# @param m int整型 
# @param n int整型 
# @return int整型
#
class Solution:
    def uniquePaths(self , m , n ):
        # write code here
        #初始化,将dp数组所有元素设置为1
        dp = [[1 for col in range(n)] for row in range(m)]
        #递推公式 dp[i][j]=dp[i-1][j]+dp[i][j-1]
        #根据递推公式确定递推方向
        for i in range(1,m):
            for j in range(1,n):
                dp[i][j]=dp[i][j]=dp[i-1][j]+dp[i][j-1]
        #最终结果为dp[m-1][n-1]
        return dp[m-1][n-1]
        
全部评论

相关推荐

已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务