当前位置:网站首页>【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
}
评判结果:
边栏推荐
- # # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
- 语言代码表
- Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
- BFS题单总结
- 抽象类和接口(学习笔记)
- Navicat cannot connect to mysql super detailed processing method
- grub 学习
- Gxlcms有声小说系统/小说听书系统源码
- 成功解决ModuleNotFoundError: No module named ‘Image‘
- Collapse legacy apps
猜你喜欢
随机推荐
【翻译】作为混沌网的LFX门徒的经验
HF2022-EzPHP复现
Navicat cannot connect to mysql super detailed processing method
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
#Dasctf July Enabler WP
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
@RequestBody、 @RequestParam 、 @PathVariable 和 @Vaild 注解
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
【导航规划】导航规划背景知识总结
ZZULIOJ:1119: 数列有序
2022.7.28
1064 Complete Binary Search Tree
关于XML的学习(一)
Abstract classes and interfaces (study notes)
EasyExcel综合课程实战
EasyExcel comprehensive course combat
解决一个Mysql的utf8编码导致的问题
编码与进制
2022.7.30