70. Climbing Stairs
[Easy] “Return the nth Fibonacci number."
Python3:
class Solution:
# def climbStairs(self, n: int) -> int:
# if n <= 2:
# return n
# else:
# res = [0, 1, 2]
# for i in range(3, n + 1):
# # calculate 1 step before, 2 steps before
# res.append(res[i - 1] + res[i - 2])
# return res[n]
def climbStairs(self, n: int) -> int:
if n <= 2:
return n
else:
two_steps_before = 1
one_steps_before = 2
for i in range(3, n + 1):
# same as above but just keep track of the last 2 previous
current = two_steps_before + one_steps_before
two_steps_before = one_steps_before
one_steps_before = current
return current