当前位置:网站首页>70. Climbing Stairs. Sol
70. Climbing Stairs. Sol
2022-07-05 22:17:00 【isee_ nh】
It is also a variant of Fibonacci sequence . The difference is that the initial items are different . If you use recursion directly, it will be more time-consuming
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): if n<=2: return n else: prev1 = self.climbStairs(1) prev2 = self.climbStairs(2) for _ in range(n-2): temp = prev2 prev2 = prev1 + prev2 prev1 = temp return prev2
边栏推荐
- AD637 usage notes
- Pinctrl subsystem and GPIO subsystem
- Create a virtual machine on VMware (system not installed)
- Server optimization of performance tuning methodology
- Sparse array [matrix]
- What about data leakage? " Watson k'7 moves to eliminate security threats
- A substring with a length of three and different characters in the leetcode simple question
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- Two stage locking protocol for concurrency control
- Dbeaver executes multiple insert into error processing at the same time
猜你喜欢
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
How to view Apache log4j 2 remote code execution vulnerability?
Common interview questions of redis factory
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Meituan dynamic thread pool practice ideas, open source
The statistics of leetcode simple question is the public string that has appeared once
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
A trip to Suzhou during the Dragon Boat Festival holiday
Sentinel production environment practice (I)
Reptile practice
随机推荐
What changes has Web3 brought to the Internet?
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
科技云报道:算力网络,还需跨越几道坎?
Common interview questions of redis factory
How can Bluetooth in notebook computer be used to connect headphones
Tips for using SecureCRT
Sentinel production environment practice (I)
微服務鏈路風險分析
Granularity of blocking of concurrency control
Blocking protocol for concurrency control
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
AD637 usage notes
Server optimization of performance tuning methodology
Business learning of mall commodity module
Pl/sql basic syntax
The simple problem of leetcode is to split a string into several groups of length K
每日刷题记录 (十四)
Poj3414 extensive search
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!