当前位置:网站首页>Sword finger offer 10- ii Frog jumping on steps
Sword finger offer 10- ii Frog jumping on steps
2022-06-24 17:45:00 【Flower boy】
One 、 Reference material
The finger of the sword Offer 10- II. The problem of frog jumping on the steps
Interview questions 10- II. The problem of frog jumping on the steps ( Recurrence method ) subject
Two 、 subject
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 Stepped steps . Ask the frog to jump on one n How many jumps are there in the steps .
The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation 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
3、 ... and 、 analysis
use f(1) Indicates the number of paths to jump to the first step
use f(2) Indicates the number of paths to jump to the second step
use f(3) Indicates the number of paths to jump to the third step
use f(4) Indicates the number of paths to jump to the fourth step
use f(n) Jump to the n Number of paths for steps
f(1): Just jump up . f(1) = 1.
f(2): Want to jump to the second step , There are two ways :
Method 1 : Jump one step from the first step to . Yes f(1) Seed path
Method 2 : Jump straight to the second step . There is a path . common f(1) + 1 = 2 Seed path .
f(3): Want to jump to the third step , There are two ways :
Method 1 : Jump two steps from the first step to . Yes f(1) Seed path .
Method 2 : Jump one step from the second step to . Yes f(2) Seed path . common f(1) + f(2) = 3 Seed path .
f(4): Want to jump to the fourth step , There are two ways :
Method 1 : Jump two steps from the second step to . Yes f(2) Seed path .
Method 2 : Jump one step from the third step to . Yes f(3) Seed path . common f(2) + f(3) = 5 Seed path .
f(5): Want to jump to the fifth step , There are two ways :
Method 1 : Jump two steps from the third step to . Yes f(3) Seed path .
Method 2 : Jump one step from the fourth step to . Yes f(4) Seed path . common f(3) + f(4) = 8 Seed path .
f(n): I want to jump to number one n A stair , There are two ways :
Method 1 : From n-2 Jump two steps to . Yes f(n-2) Seed path .
Method 2 : From n-1 Step by step to reach . Yes f(n-1) Seed path . common f(n-2) + f(n-1) Seed path .
Four 、 Refer to the answer
class Solution {
public:
int numWays(int n) {
if(n == 0) return 1;
if(n <= 2) return n;
int a = 1, b = 2, c = 0;
for (int i = 3; i <= n; ++i) {
c = (a + b) % 1000000007;
a = b;
b = c;
}
return c;
}
};
边栏推荐
- About swagger
- On software requirement analysis
- Mengyou Technology: tiktok current limiting? Teach you to create popular copywriting + popular background music selection
- Conditional competition overview
- How to troubleshoot and solve the problem that the ultra-low delay security live broadcast system webrtc client plays no audio in the browser?
- H265 video streaming web page without plug-in player easywasmlayer Troubleshooting and solution of JS unable to set cover photo
- Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters
- "Competition" and "opportunity" hidden in security operation in the cloud Era
- Tencent cloud won the "trusted cloud technology best practice - virtualization"
- -Bash: wget: command not found
猜你喜欢

On software requirement analysis

Constantly changing the emergency dialing of harmonyos ETS during the new year
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters

It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements

13 ways to reduce the cost of cloud computing
Using flex to implement common layouts

Six configuration management tools that administrators must know

High quality defect analysis: let yourself write fewer bugs

NVM download, installation and use
SQL basic tutorial (learning notes)
随机推荐
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
Open up the construction of enterprise digital procurement, and establish a new and efficient service mode for raw material enterprises
Leveldb source code analysis -- log file format
布隆过滤器综述文章论文阅读:Optimizing Bloom Filter: Challenges, Solutions, and Comparisons
Specification for self test requirements of program developers
Dunhuang Research Institute and Tencent have launched a new strategic cooperation to take you around the digital new silk road with AI
Leveldb source code analysis -- writing data
You don't know about this inspection platform. It's a big loss!
Service not found troubleshooting and resolution of error messages in the secondary development of the source code of the open source platform easydarwin
How to use SEO to increase the inquiry volume?
Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
13 ways to reduce the cost of cloud computing
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
Operation and maintenance guide | cos back source setting practice
A set of IM architecture technology dry goods for 100 million users (Part 2): reliability, orderliness, weak network optimization, etc
NVM download, installation and use
Use cloud development to make a login free resource navigation applet!
About swagger
Yum to install warning:xxx: header V3 dsa/sha1 signature, key ID 5072e1f5: nokey
Noi Mathematics: solution of quadratic congruence equation