当前位置:网站首页>【LeetCode】70. 爬楼梯 - Go 语言题解
【LeetCode】70. 爬楼梯 - Go 语言题解
2022-07-30 22:48:00 【想变厉害的大白菜】
一、题目描述
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入:n = 3
输出:3
解释:有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
提示:
1 <= n <= 45
题目链接:https://leetcode.cn/problems/climbing-stairs/
二、解题思路
观察:
1级台阶:1
2级台阶:1+1或者2
3级台阶:1+2或者1+1+1或者2+1
…
所以上 n 级台阶等于 从n-1级上1级 加上 从n-2级上2级。
那么上 n 级台阶的上法等于n-1级的上法加上n-2级的上法,也就是:f(x)=f(x−1)+f(x−2)。
递推公式:0,0,1,1,2,3,5,8…
三、我的题解
Golang 代码:
func climbStairs(n int) int {
a,b,c := 0,0,1
for n > 0 {
a = b
b = c
c = a+b
n--
}
return c
}
评判结果:

边栏推荐
猜你喜欢
随机推荐
CISP-PTE Zhenti Demonstration
openim支持十万超级大群
力扣题(2)—— 两数相加
Gxlcms有声小说系统/小说听书系统源码
EasyExcel comprehensive course combat
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
2022/07/30 学习笔记 (day20) 面试题积累
语言代码表
BFS题单总结
Navicat cannot connect to mysql super detailed processing method
MySQL 5.7 detailed download, installation and configuration tutorial
【翻译】作为混沌网的LFX门徒的经验
Go的Gin框架学习
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
Excel basic study notes
[MySQL] Mysql transaction and authority management
DFS题单以及模板汇总
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
Computer shortcut icon whitening solution
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)








