当前位置:网站首页>力扣——青蛙跳台阶问题
力扣——青蛙跳台阶问题
2022-08-11 04:13:00 【cbys-1357】
题目:
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
示例 1:
输入:n = 2
输出:2示例 2:
输入:n = 7
输出:21示例 3:
输入:n = 0
输出:1设跳上 n 级台阶有 f(n) 种跳法。在所有跳法中,青蛙的最后一步只有两种情况: 跳上 1 级或 2级台阶。
当为 1 级台阶: 剩 n−1 个台阶,此情况共有 f(n−1) 种跳法;
当为 2 级台阶: 剩 n−2 个台阶,此情况共有 f(n−2) 种跳法。
f(n) 为以上两种情况之和,即 f(n)=f(n−1)+f(n−2) ,以上递推性质为斐波那契数列。本题可转化为 求斐波那契数列第 n 项的值 ,与 斐波那契数列 等价,唯一的不同在于起始数字不同。
青蛙跳台阶问题: f(0)=1 , f(1)=1 , f(2)=2
斐波那契数列问题: f(0)=0 , f(1)=1 , f(2)=1 class Solution {
public int numWays(int n) {
int a = 1, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}
博主本人把这道题题解放到,不是表达这道题很难,用了什么高级的算法来绝决,而是提醒自己,自己有多么多么笨,在第一下看到这道题的时候,我的内心一直想的是什么排列组合,来计算有多少种,而且还出现错误,想想自己,。。。,害,把这题题解放到我的一篇文章中,是提醒自己,别把自己蠢死了,越学越笨。。。说句实话,我确实一直没有想到,最后还是看解析才知道。原来是这样子。
最后到这也就结束,在此谢谢大家能够阅读到最后,非常感谢!!!
边栏推荐
- js 将字符串作为js执行代码使用
- [C Language] Getting Started
- uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
- 【组成原理 九 CPU】
- Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
- What is machine learning?Explain machine learning concepts in detail
- LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
- 获取Qt的安装信息:包括安装目录及各种宏地址
- What are port 80 and port 443?What's the difference?
- 【FPGA】day20-I2C读写EEPROM
猜你喜欢

Multi-serial port RS485 industrial gateway BL110

LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
![[C Language] Getting Started](/img/5e/484e3d426a6f1cc0d792a9ba330695.png)
[C Language] Getting Started

Echart地图的省级,以及所有地市级下载与使用

Get Qt installation information: including installation directory and various macro addresses

Detailed explanation of VIT source code

破解事务性工作瓶颈,君子签电子合同释放HR“源动力”!

【yolov7系列三】实战从0构建训练自己的数据集

Read the article, high-performance and predictable data center network

无线电射频能量的收集
随机推荐
Get Qt installation information: including installation directory and various macro addresses
Mysql:设置主键自动增长起始值
Power Cabinet Data Monitoring RTU
直播平台开发,Flutter,Drawer侧滑
快速使用UE4制作”大场景游戏“
机器学习怎么学?机器学习流程
set_new_handler(0)是什么意思?有什么用?
uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
【FPGA】abbreviation
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
What is third-party payment?
【FPGA】day22-SPI协议回环
Detailed explanation of VIT source code
LeetCode刷题第16天之《239滑动窗口最大值》
Get the length of the linked list
洛谷P4032 火锅盛宴
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
js uses the string as the js execution code
【服务器安装Redis】Centos7离线安装redis
rub the heat - do not open