码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-mid-dynamic programming-62. Unique Paths

时间:2019-06-05 12:35:21      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:limit   pre   ret   dynamic   list   sel   obj   object   type   

mycode   time limited

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if not m or not n :
            return 0
        if m == 1 or n == 1:
            return 1
        return self.uniquePaths(m-1,n) + self.uniquePaths(m,n-1)

参考:

思路: 类似于金字塔那个题,考虑上一步即可

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if m == 1 and n == 1:
            list = [[1]]
        elif m == 1 and n > 1:
            list = [[1 for i in range(n)]]
        elif m > 1 and n == 1:
            list = [[1] for i in range(m)]
        else:
            list = [[0 for i in range(n)] for i in range(m)]
            for i in range(0, n):
                list[0][i] = 1
            for i in range(0, m):
                list[i][0] = 1
            for i in range(1, m):
                for j in range(1, n):
                    list[i][j] = list[i-1][j] + list[i][j-1]
        return list[m-1][n-1]

 

leetcode-mid-dynamic programming-62. Unique Paths

标签:limit   pre   ret   dynamic   list   sel   obj   object   type   

原文地址:https://www.cnblogs.com/rosyYY/p/10978872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!