当前位置:网站首页>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];}};边栏推荐
猜你喜欢

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

Chapter 12 Bayesian Networks

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

苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品

吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)

mySQL数据库初始化失败,有谁可以指导一下吗

Creo 9.0 基准特征:基准平面

SQL语句查询字段内重复内容,并按重复次数加序号

XSS靶机通关以及XSS介绍

sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
随机推荐
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
写出了一个CPU占用极高的代码后引发的思考
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
Xcode 12 ld: symbol(s) not found for architecture armv64
学习笔记14--机器学习在局部路径规划中的应用
Hbuilder 学习使用中的一些记录
使用 External Secrets Operator 安全管理 Kubernetes Secrets
Controlling number and letter input in ASP
Luogu P4588: [TJOI2018]数学计算
交换机端口的三种类型详解与hybrid端口实验
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
七夕看什么电影好?爬取电影评分并存入csv文件
tensorflow.keras无法引入layers
16 kinds of fragrant rice recipes
路由----router
SQL语句查询字段内重复内容,并按重复次数加序号
ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
love is a sad song
ts/js function pass parameter with function writing
网页直接访问链接不让安全中心拦截