Suppose you are climbing stairs. It takes n steps to reach the top of the building.
You can climb 1 or 2 steps each time. How many different ways can you get to the top of a building?
Note: n is given a positive integer.
Example 1:
Input: 2 Output: 2 Explanation: There are two ways to climb to the top of the building. 1. 1 step + 1 step 2. 2 steps
Example 2:
Input: 3 Output: 3 Explanation: There are three ways to climb to the top of the building. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
python code
class Solution: def climbStairs(self, n): """ :type n: int :rtype: int """ #Similar to Fibonacci practice result=[1,1]#Initialize the first level and the second level if n>=2: for i in range(2,n+1): result.append(result[i-1]+result[i-2])#The current result can come from the previous step or from the previous step, in two cases return result[n]