当前位置:网站首页>[LeetCode] 爬楼梯【70】
[LeetCode] 爬楼梯【70】
2022-07-01 00:37:00 【山茶花开时。】
问题: 假设你正在爬楼梯,需要n阶才能到达楼顶。每次你可以爬1或2个台阶,你有多少种不同的方法可以爬到楼顶呢?
示例1
输入: n = 2
输出: 2
解释: 有两种方法可以爬到楼顶
① 1阶+1阶
② 2阶
示例2
输入: n = 3
输出: 3
解释: 有三种方法可以爬到楼顶。
①1阶+1阶+1阶
②1阶+2阶
③2阶+1阶
# 解法1
def climbStairs(n):
if n <= 2:
return n
else:
return climbStairs(n-1) + climbStairs(n-2)
# 解法2
def climbStairs(n):
arr = [1,2]
if n <= 2:
return arr[n - 1]
else:
# n大于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
边栏推荐
- P4学习——p4runtime
- Problem solving: how to manage thread_local pointer variables
- The principle and related problems of acid in MySQL
- ArrayList analysis 1-cycle, capacity expansion, version
- HDU 2488 A Knight's Journey(DFS)
- 魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
- Vnctf 2022 cm CM1 re reproduction
- 2022-2028 global single travel industry research and trend analysis report
- Teach you how to use Hal library to get started -- become a lighting master
- High quality pump SolidWorks model material recommended, not to be missed
猜你喜欢
Redis - understand the master-slave replication mechanism
2022-2028 global public address fire alarm system industry research and trend analysis report
Host FL Studio fruit music production daw20.9
What if the disk of datanode is full?
Date类的实现
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
Docsify building a personal minimalist knowledge warehouse
C#生成putty格式的ppk文件(支持passphrase)
C # Generate PPK files in Putty format (passthrough support)
2022-2028 global herbal diet tea industry research and trend analysis report
随机推荐
C language file operation for conquering C language
Join table query select generation
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
Self examination before school starts
Redis - how to understand publishing and subscribing
P4学习——Basic Tunneling
2022 is half way through. It's hard to make money
The principle of journal node
Oracle table creation and management
Vulnerability discovery - App application vulnerability probe type utilization and repair
Analysis of blocktoken principle
2022-2028 global elevator emergency communication system industry research and trend analysis report
Deployment of mini version message queue based on redis6.0
Mindjet mindmanager2022 mind map decompression installer tutorial
HDU 2488 A Knight's Journey(DFS)
Redis based distributed lock
Oracle temporary table explanation
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
Pytorch auto derivation
2022就要过去一半了,挣钱好难