class Solution(object):
# def climbStairs(self, n):
# """
# :type n: int
# :rtype: int
# """
# dp = [0] * (n + 1)
# dp[0] = 1
# dp[1] = 1
# for i in range(2, n + 1):
# dp[i] = dp[i - 2] + dp[i- 1]
# return dp[n]
def climbStairs(self, n):
if n <= 1:
return 1
dp = [1] * 2
for i in range(2, n + 1):
dp[1], dp[0] = dp[1] + dp[0], dp[1]
return dp[1]
# C = {1: 1, 2: 2}
# def climbStairs(self, n):
# """
# :type n: int
# :rtype: int
# """
# if n in Solution.C:
# return Solution.C[n]
# else:
# result = Solution.C.get(n - 1, self.climbStairs(n - 1)) + \
# Solution.C.get(n - 2, self.climbStairs(n - 2))
# Solution.C[n] = result
# return result
Download Climbing Stairs.pyLeetcode 070 Climbing Stairs problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.