当前位置:网站首页>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];}};边栏推荐
- ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
- IT研发/开发流程规范效能的思考总结
- leetcode 剑指 Offer 10- I. 斐波那契数列
- XSS靶机通关以及XSS介绍
- Rotation of the displayed value on the button
- 请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
- 在colab里怎样读取google drive数据
- 全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
- 让程序员崩溃的N个瞬间(非程序员误入)
- 随时牵手 不要随意分手[转帖]
猜你喜欢

使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)

Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)

国际原子能机构总干事称乌克兰扎波罗热核电站安全形势堪忧

让程序员崩溃的N个瞬间(非程序员误入)

pytorch余弦退火学习率CosineAnnealingLR的使用

Code Audit - PHP

学习笔记14--机器学习在局部路径规划中的应用

XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法

Chapter 12 Bayesian Networks

原型&原型链
随机推荐
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
DPU — 功能特性 — 管理系统的硬件卸载
Luogu P1908: 逆序对 [树状数组]
512色色谱图
DTcloud 装饰器
DPU — 功能特性 — 安全系统的硬件卸载
苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
百行代码发射红心,程序员何愁命不中女朋友!
画法几何及工程制图考试卷A卷
线程之Happens-before规则
DNS 查询原理详解
Detailed explanation of DNS query principle
树状数组模版+例题
按钮上显示值的轮流切换
The Secrets of the Six-Year Team Leader | The Eight Most Important Soft Skills of Programmers
DPU — 功能特性 — 存储系统的硬件卸载
Undefined symbols for architecture arm64解决方案
宝塔实测-搭建中小型民宿酒店管理源码
并发之CAS
egg框架