当前位置:网站首页>[leetcode] climb stairs [70]
[leetcode] climb stairs [70]
2022-07-01 01:20:00 【When Camellia blossoms.】
problem : Suppose you're climbing the stairs , need n To reach the roof . Every time you climb 1 or 2 A stair , How many different ways can you climb to the top of the building ?
Example 1
Input : n = 2
Output : 2
explain : There are two ways to climb to the top
① 1 rank +1 rank
② 2 rank
Example 2
Input : n = 3
Output : 3
explain : There are three ways to climb to the top .
①1 rank +1 rank +1 rank
②1 rank +2 rank
③2 rank +1 rank
# solution 1
def climbStairs(n):
if n <= 2:
return n
else:
return climbStairs(n-1) + climbStairs(n-2)
# solution 2
def climbStairs(n):
arr = [1,2]
if n <= 2:
return arr[n - 1]
else:
# n Greater than 2
for i in range(2,n):
arr.append(arr[i - 1] + arr[i - 2])
return arr[n - 1]
climbStairs(2) # 2
climbStairs(3) # 3
边栏推荐
- Share your own terminal DIY display banner
- Two position relay st2-2l/ac220v
- 解析融合学科本质的创客教育路径
- What if the disk of datanode is full?
- [original] PLSQL index sorting optimization
- Analyze the maker education path integrating the essence of discipline
- 【学习笔记】倍增 + 二分
- Exercises on recursion in C language
- Detailed analysis of operators i++ and ++i in JS, i++ and ++i
- None of the following candidates is applicable because of a receiver type mismatch
猜你喜欢
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
P4 learning - p4runtime
Two position relay st2-2l/ac220v
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
What if the disk of datanode is full?
Windows环境下安装MongoDB数据库
人穷志不短,穷学生也能玩转树莓派
DX-11Q信号继电器
【网络丢包,网络延迟?这款神器帮你搞定所有!】
Golang treasure house recommendation
随机推荐
Oracle data integrity
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
ORB-SLAM2源码学习(二)地图初始化
Docker deployment MySQL 8
pull_ to_ refresh
冲击继电器ZC-23/DC220V
JS方法大全的一个小文档
Kongyiji's first question: how much do you know about service communication?
Open3D 点云包围盒
分割链表[先取next再斩断链表防止断链]
Principes de formation de la programmation robotique
小程序自定义宫格
用Steam教育启发学生多元化思维
Q弹松软的大号吐司,带来更舒服的睡眠
Open3D 点云颜色渲染
User defined annotation implementation verification
[learning notes] double + two points
Win11安装redis 数据库以及redis desktop manager的下载
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?