当前位置:网站首页>[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 learning - Basic tunneling
- What if the disk of datanode is full?
- Join table query select generation
- ArrayList analysis 1-cycle, capacity expansion, version
- Plot size and resolution with R markdown, knitr, pandoc, beamer
- Chapter 53 overall understanding of procedures from the perspective of business logic implementation
- HDU 2488 A Knight's Journey(DFS)
- ArrayList analysis 1-cycle, capacity expansion, version
- Pytorch auto derivation
- Basic data structure of redis
猜你喜欢

Redis - cache penetration, cache breakdown, cache avalanche

Introduction to ES6 promise, new features of ES7 and es8 async and await

HDU 2488 A Knight's Journey(DFS)

Implementation of date class

2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report

20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute

优质的水泵 SolidWorks模型素材推荐,不容错过

2022-2028 global retro glass industry research and trend analysis report

Stack frame

【原创】 PLSQL 索引排序优化
随机推荐
ArrayList分析1-循环、扩容、版本
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
Luogu p1144 shortest circuit count
Problem solving: how to manage thread_local pointer variables
P4学习——p4runtime
P4 learning - p4runtime
Oracle temporary table explanation
双链表:初始化 插入 删除 遍历
A proper job is a good job
Examples of topological sequences
2022-2028 global PTFE lined valve industry research and trend analysis report
NE555波形发生器手把手教程之NE555内部结构(一)
Redis - cache penetration, cache breakdown, cache avalanche
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
写给 5000 粉丝的一封信!
Redis - understand the master-slave replication mechanism
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
Bugku CTF daily one question dark cloud invitation code
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
合适的工作就是好工作