Skip to content

Climbing Stairs

LeetCode

01 · Question

You are climbing a staircase. It takes n steps to reach the top. Each time you can climb either 1 or 2 steps. Return how many distinct ways you can climb to the top.

02 · Solution

Reference solution

1def climbStairs(n: int) -> int:
2 if n <= 2:
3 return n
4 a, b = 1, 2
5 for _ in range(3, n + 1):
6 a, b = b, a + b
7 return b