当前位置:网站首页>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 <= 45class 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
边栏推荐
- Business learning of mall commodity module
- Leetcode simple question check whether all characters appear the same number of times
- Common interview questions of redis factory
- Sparse array [matrix]
- Daily question brushing record (XIV)
- Shell script, awk condition judgment and logic comparison &||
- CA certificate trampled pit
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- Sentinel production environment practice (I)
- 元宇宙中的三大“派系”
猜你喜欢

Leetcode simple question: find the nearest point with the same X or Y coordinate

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

Damn, window in ie open()

Business learning of mall order module

Leetcode simple question: the minimum cost of buying candy at a discount

AD637使用笔记

Nacos 的安装与服务的注册

Interprocess communication in the "Chris Richardson microservice series" microservice architecture

Three "factions" in the metauniverse

EBS Oracle 11g cloning steps (single node)
随机推荐
每日刷题记录 (十四)
How to add new fields to mongodb with code (all)
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
Storage optimization of performance tuning methodology
Bitbucket installation configuration
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Leetcode simple question: check whether each row and column contain all integers
Oracle views the data size of a table
Leetcode simple question ring and rod
How to use tensorflow2 for cat and dog classification and recognition
Summary of El and JSTL precautions
K210 learning notes (IV) k210 runs multiple models at the same time
Ad637 notes d'utilisation
Nacos 的安装与服务的注册
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Technology cloud report: how many hurdles does the computing power network need to cross?
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Draw a red lantern with MATLAB
Implementation technology of recovery