mycode 65%
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 1: return 1 if n == 2: return 2 i = 3 dic = {1:1,2:2} while i < n+1: dic[i] = dic[i-2] + dic[i-1] i += 1 return dic[n]
下面这个会超时。。。
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 1: return 1 if n == 2: return 2 return self.climbStairs(n-1) + self.climbStairs(n-2)
参考
class Solution(object): def climbStairs(self, n): a, b = 1, 1 for i in range(n): a, b = b, a + b return a