You are climbing a staircase. It takes n steps to reach the top.
Each time you can climb either 1 or 2 steps. In how many distinct ways can you climb to the top? Two ways are different when their moves come in a different order: climbing 1 step and then 2 is not the same as climbing 2 and then 1.
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
Example 3:
Input: n = 4 Output: 5 Explanation: There are five ways to climb to the top: 1. 1 step + 1 step + 1 step + 1 step 2. 1 step + 1 step + 2 steps 3. 1 step + 2 steps + 1 step 4. 2 steps + 1 step + 1 step 5. 2 steps + 2 steps
Constraints:
1 <= n <= 45- The answer always fits in a 32-bit integer.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
