当前位置:网站首页>算法-- 爬楼梯(Kotlin)
算法-- 爬楼梯(Kotlin)
2022-07-06 05:18:00 【小米科技Android 研发曹新雨】
题目
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入:n = 3
输出:3
解释:有三种方法可以爬到楼顶。
1 阶 + 1 阶 + 1 阶
1 阶 + 2 阶
2 阶 + 1 阶
提示:
1 <= n <= 45
https://leetcode.cn/problems/climbing-stairs/
解决思路
- 本问题其实常规解法可以分成多个子问题,爬第n阶楼梯的方法数量,等于 2 部分之和
爬上 n-1阶楼梯的方法数量。因为再爬1阶就能到第n阶
爬上 n-2阶楼梯的方法数量,因为再爬2阶就能到第n阶
所以我们得到公式 dp[n] = dp[n-1] + dp[n-2]
同时需要初始化 dp[0]=1 和 dp[1]=1d
时间复杂度:O(n)
2.既然 dp[n] = dp[n-1] + dp[n-2]
那么我们可以用滑动数组去解决
解决方法
fun climbStairs(n: Int): Int {
val intArray = IntArray(n + 1)
intArray[0] = 1
intArray[1] = 1
for (i in 2..n) {
intArray[i] = intArray[i - 1] + intArray[i - 2]
}
return intArray[n]
}
fun climbStairs2(n: Int): Int {
var q = 1
var p = 1
var result = 1
for (i in 2..n) {
result = q + p
q = p
p = result
}
return result
}
总结
1.这个题一下没有想出来怎么做
看了一下题解才做出来
边栏推荐
- First acquaintance with CDN
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Upload nestjs configuration files, configure the use of middleware and pipelines
- Mongodb basic knowledge summary
- Vulhub vulnerability recurrence 69_ Tiki Wiki
- Nacos TC setup of highly available Seata (02)
- Summary of redis basic knowledge points
- nacos-高可用seata之TC搭建(02)
- Acwing week 58
- 2022半年总结
猜你喜欢
![[classic example] binary tree recursive structure classic topic collection @ binary tree](/img/39/0319c4be43716f927b9d98d89f7655.jpg)
[classic example] binary tree recursive structure classic topic collection @ binary tree
![[untitled]](/img/7e/d0724193f2f2c8681a68bda9e08289.jpg)
[untitled]

【LeetCode】18、四数之和

Check the useful photo lossless magnification software on Apple computer

Compilation et connexion de shader dans games202 - webgl (comprendre la direction)

【torch】|torch. nn. utils. clip_ grad_ norm_

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Lepton 无损压缩原理及性能分析

你需要知道的 TCP 三次握手

Pointer classic written test questions
随机推荐
Sorting out the knowledge points of multicast and broadcasting
Three methods of Oracle two table Association update
[buuctf.reverse] 159_[watevrCTF 2019]Watshell
Rce code and Command Execution Vulnerability
Pointer classic written test questions
01. 开发博客项目之项目介绍
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
F12 solve the problem that web pages cannot be copied
Pickle and savez_ Compressed compressed volume comparison
[noip2008 improvement group] stupid monkey
Three. JS learning - light and shadow (understanding)
C进阶-数据的存储(上)
TCP three handshakes you need to know
GAMES202-WebGL中shader的编译和连接(了解向)
[mask requirements of OSPF and Isis in multi access network]
Promotion hung up! The leader said it wasn't my poor skills
C Advanced - data storage (Part 1)
idea一键导包
Hyperledger Fabric2. Some basic concepts of X (1)
MySQL time processing