zoukankan      html  css  js  c++  java
  • leetcode-mid-dynamic programming-62. Unique Paths

    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]
  • 相关阅读:
    2016.7.31整机升级计划
    UVa 1588
    UVa1587
    Jzoj4714 公约数
    Jzoj4714 公约数
    Jzoj4713 A
    Jzoj4713 A
    Jzoj4711 Binary
    Jzoj4711 Binary
    Jzoj4710 Value
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10978872.html
Copyright © 2011-2022 走看看