You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
- 1 <= n <= 45
class Solution:
def climbStairs(self, n: int) -> int:
one=1
two=1
for i in range(n-1):
temp=one
one=one+two
two=temp
return one
'알고리즘 문제 > Leetcode' 카테고리의 다른 글
119. Pascal's Triangle II (0) | 2022.12.08 |
---|---|
118. Pascal's Triangle (0) | 2022.12.08 |
11. Container With Most Water (0) | 2022.12.07 |
328. Odd Even Linked List (0) | 2022.12.06 |
2389. Longest Subsequence With Limited Sum (0) | 2022.12.05 |