当前位置:网站首页>leetcode refers to Offer 10- II. Frog jumping steps
leetcode refers to Offer 10- II. Frog jumping steps
2022-08-05 09:05:00 【Promising_mi】
A frog can jump up 1 or 2 steps at a time.Find how many ways the frog can jump up a n steps.
The answer needs to be modulo 1e9+7 (1000000007). If the initial calculation result is: 1000000008, please return 1.
Example 1:
Input: n = 2
Output: 2
Example 2:
Input: n = 7
Output: 21
Example 3:
Input: n = 0
Output: 1
Prompt:
0 <= n <= 100
Thinking:
In fact, the idea of the Fibonacci sequence is the same. There are f(n-1) and f(n-2) methods to reach the nth step.
It is enough to jump once from the n-1th to the nth order, so there is only one way, and from the n-2 to the nth order, you can jump 2 orders at a time or jump 2 times at a time, and thenThe first one (the method of jumping one order at a time) has been counted in the method from n-1 to the nth order.
So from n-2 to the nth order there is only one way to jump to order 2.
So f(n) = f(n-1)+f(n-2)
class Solution {public:int numWays(int n) {/*int a=1,b=2; //The code commented out here uses dynamic programming int h; for(int i=2;i<=n;++i){ h=a+b; h%=1000000007; a=b; b=h; } return a; */if(n<=1)return 1;int dp[n+1];dp[1]=1;dp[2]=2;for(int i=3;i<=n;++i){dp[i]=dp[i-1]span>+dp[i-2]; //hereUse arrays to store values and avoid repeated operationsdp[i]%=1000000007;}return dp[n];}};边栏推荐
- 汇编语言(8)x86内联汇编
- php fails to write data to mysql
- [Structure internal power practice] Structure memory alignment (1)
- 并发之CAS
- mySQL数据库初始化失败,有谁可以指导一下吗
- Controlling number and letter input in ASP
- Luogu: P2574 XOR的艺术 [线段树]
- k-nearest neighbor fault monitoring based on multi-block information extraction and Mahalanobis distance
- 手机上流行的各类谜语
- Adb authorization process analysis
猜你喜欢

Three solutions to solve cross-domain in egg framework

嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)

Chapter 12 贝叶斯网络

使用 External Secrets Operator 安全管理 Kubernetes Secrets

全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了

Dynamic memory development (C language)

pnpm 是凭什么对 npm 和 yarn 降维打击的

工程制图直线投影练习

施一公:科学需要想象,想象来自阅读
![[Structure internal power practice] Structure memory alignment (1)](/img/31/4ddc16810da8238ac95a93d007e12e.png)
[Structure internal power practice] Structure memory alignment (1)
随机推荐
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
接口全周期的生产力利器Apifox
明天去订票,准备回家咯~~
ts/js 函数传参带函数写法
mySQL数据库初始化失败,有谁可以指导一下吗
8.4 Summary of the mock competition
DPU — 功能特性 — 存储系统的硬件卸载
16 kinds of fragrant rice recipes
Detailed explanation of DNS query principle
在colab里怎样读取google drive数据
leetcode 剑指 Offer 10- I. 斐波那契数列
基因数据平台
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
selectPage 动态改变参数方法
按钮上显示值的轮流切换
树状数组模版+例题
Hbuilder 学习使用中的一些记录
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
Xcode 12 ld: symbol(s) not found for architecture armv64