Easy python bottom-up


  • 0
    S
    class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        pre = [1, 2]
        if n<3:
            return pre[n-1]
        i=3
        while(i<=n):
            temp = pre[0]
            pre[0]=pre[1]
            pre[1] = temp+pre[0]
            i=i+1
        return pre[-1]

  • 3
    M

    maybe:

    class Solution(object):
        def climbStairs(self, n):
            """
            :type n: int
            :rtype: int
            """
            m = 0
            l = 1
            for i in range(n):
                m, l = l, m + l
            return l

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.