当前位置:网站首页>力扣——青蛙跳台阶问题
力扣——青蛙跳台阶问题
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;
}
}
博主本人把这道题题解放到,不是表达这道题很难,用了什么高级的算法来绝决,而是提醒自己,自己有多么多么笨,在第一下看到这道题的时候,我的内心一直想的是什么排列组合,来计算有多少种,而且还出现错误,想想自己,。。。,害,把这题题解放到我的一篇文章中,是提醒自己,别把自己蠢死了,越学越笨。。。说句实话,我确实一直没有想到,最后还是看解析才知道。原来是这样子。
最后到这也就结束,在此谢谢大家能够阅读到最后,非常感谢!!!
边栏推荐
猜你喜欢
How to learn machine learning?machine learning process
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
(转)JVM中那些区域会发生OOM?
Get Qt installation information: including installation directory and various macro addresses
What is Machine Reinforcement Learning?What is the principle?
随机推荐
Use jackson to parse json data in detail
蹭个热度-请勿打开
2022-08-10 The sixth group Hiding spring study notes
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
机器学习中什么是集成学习?
常见布局效果实现方案
一文读懂 高性能可预期数据中心网络
拼多多店铺营业执照相关问题
LeetCode刷题第10天字符串系列之《125回文串验证》
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
【FPGA】SDRAM
直播平台开发,Flutter,Drawer侧滑
es-head plugin insert query and conditional query (5)
洛谷P2370 yyy2015c01 的 U 盘
Build Zabbix Kubernetes cluster monitoring platform
The FTP error code list
【FPGA】abbreviation
redis按照正则批量删除key
Binary tree related code questions [more complete] C language
干货:服务器网卡组技术原理与实践