Suppose you are climbing stairs. N order you need to get to the roof.
Every time you can climb one or two steps. How many different ways can climb to the roof of it?
Note: Given n is a positive integer.
Example 1:
Input: 2
Output: 2
explanation: There are two methods can climb the roof.
1.1 + 1-order-order
2.2 Order
Example 2:
Input: 3
Output: 3
Explanation: There are three methods can climb to the roof.
1.1 + 1 order + 1-order-order
2.1 + 2-order order
3.2 + 1-order-order
class Solution: def climbStairs(self, n: int) -> int: prev,current = 0,1 # n = 1 : 1 # n = 2 : 2 # n = 3 : 3 = 2 + 1 # n = 4 : 5 = 3 + 2 #........ for i in range(n): prev,current = current,prev+current return current
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/climbing-stairs
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.