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

Undefined symbols for architecture arm64解决方案

画法几何及工程制图考试卷A卷

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

MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

基因数据平台

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

汇编语言(8)x86内联汇编

sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?

【LeetCode】623. 在二叉树中增加一行

Thinking and summary of the efficiency of IT R&D/development process specification
随机推荐
C语言-数组
“充钱”也难治快手的“亏亏亏”?
工程制图直线投影练习
写出了一个CPU占用极高的代码后引发的思考
Chapter 12 Bayesian Networks
使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)
学习笔记14--机器学习在局部路径规划中的应用
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
egg框架
国际原子能机构总干事称乌克兰扎波罗热核电站安全形势堪忧
Code Audit - PHP
Iptables implementation under the network limited (NTP) synchronization time custom port
egg框架中解决跨域的三种方案
动态内存开辟(C语言)
Walk 100 trick society
Pagoda measurement - building small and medium-sized homestay hotel management source code
routing----router
控制器-----controller
工程制图试题
汇编语言(8)x86内联汇编