当前位置:网站首页>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];}};
边栏推荐
- The color of life divine
- Three solutions to solve cross-domain in egg framework
- DNS 查询原理详解
- DPU — 功能特性 — 安全系统的硬件卸载
- 线程之Happens-before规则
- selectPage 动态改变参数方法
- What is the connection and difference between software system testing and acceptance testing? Professional software testing solution recommendation
- tear apart loneliness
- mySQL数据库初始化失败,有谁可以指导一下吗
- 【无标题】目录
猜你喜欢
Detailed explanation of DNS query principle
基因数据平台
宝塔实测-搭建中小型民宿酒店管理源码
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
Three solutions to solve cross-domain in egg framework
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
动态库之间回调函数使用
学习笔记14--机器学习在局部路径规划中的应用
Dynamic memory development (C language)
随机推荐
Controller-----controller
Thinking after writing a code with a very high CPU usage
Luogu P1966: [NOIP2013 提高组] 火柴排队 [树状数组+逆序对]
Walk 100 trick society
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
8.4 Summary of the mock competition
Luogu P1908: 逆序对 [树状数组]
CCVR基于分类器校准缓解异构联邦学习
Linux导出数据库数据到硬盘
16种香饭做法全攻略
Pagoda measurement - building small and medium-sized homestay hotel management source code
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
动态内存开辟(C语言)
让程序员崩溃的N个瞬间(非程序员误入)
Iptables implementation under the network limited (NTP) synchronization time custom port
程序员的七种武器
基于多块信息提取和马氏距离的k近邻故障监测