当前位置:网站首页>Leetcode skimming -- bit by bit record 017
Leetcode skimming -- bit by bit record 017
2022-07-24 21:04:00 【Robust least squares support vector machine】
17. The finger of the sword Offer 10-II. The problem of frog jumping on the steps
requirement
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.
Ideas
Let's jump up n The steps have f(n) Jump . Of all the jumps , There are only two situations for the frog's last step : Jump on 1 Grade or 2 Stepped steps .
- Situation 1 : When it comes to 1 Stepped steps , Remnant n-1 A stair , There are a total of f(n-1) Jump ;
- Situation two : When it comes to 2 Stepped steps , Remnant n-2 A stair , There are a total of f(n-2) Jump .
namely f(n) Is the sum of the above two situations , f(n)=f(n-1)+f(n-2) , The above recurrence property is Fibonacci sequence . however , The problem of frog jumping on the steps : f(0)=1 , f(1)=1 , f(2)=2.
Problem solving
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
int numWays(int n) {
if (n == 0) {
return 1; // If you ask f(0) Then return directly 1 , initialization f(0)
}
vector<int> dp(n + 1, 1); // initialization dp list
dp[1] = 1; // initialization f(1)
for (int i = 2; i <= n; i++) {
dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007; // State transition calculation f(2), f(3), ..., f(n)
}
return dp[n]; // return f[n]
}
};
void test01()
{
Solution s;
for (int i = 0; i < 10; i++) {
int n = s.numWays(i);
cout << n << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

I hope this article can help you , If there is anything wrong with the above , Welcome to correct
Sharing determines the height , Learn to widen the gap
边栏推荐
- npm Warn config global `--global`, `--local` are deprecated. Use `--location=global` instead
- 2787: calculate 24
- API data interface for historical data of A-share index
- Docker builds redis and clusters
- Top 10 in China's HCI software market: Huawei, Xinhua, Shenxin, VMware, Lenovo, smartx, Inspur, Qingyun, lutanli, dawn
- Upgrade appium automation framework to the latest 2.0
- Lecun proposed that mask strategy can also be applied to twin networks based on vit for self supervised learning!
- C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms
- Acwing 94. recursive implementation of permutation enumeration
- 1. Mx6u-alpha development board (buzzer experiment)
猜你喜欢

Lua environment configuration
![[summary of Feature Engineering] explain what features are and the steps of feature engineering](/img/29/f9643affd3aaf9d83c5b81394d29a7.png)
[summary of Feature Engineering] explain what features are and the steps of feature engineering

Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down

C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms

How to test WebService interface

How to set appium script startup parameters

ma.glasnost.orika. MappingException:No converter registered for conversion from Date to LocalDateTime

How does starknet change the L2 landscape?
Hilditch refinement (implementation I)

90% of people don't know the most underestimated function of postman!
随机推荐
Career development suggestions shared by ten CIOs
[summary of Feature Engineering] explain what features are and the steps of feature engineering
怎么在中金证券购买新课理财产品?收益百分之6
How about Urumqi Shenwan Hongyuan securities account opening? Is it safe?
Lecun proposed that mask strategy can also be applied to twin networks based on vit for self supervised learning!
[install PG]
[training Day9] light tank [dynamic planning]
[training Day8] tent [mathematics] [DP]
[JVM] selection of garbage collector
[msp430g2553] graphical development notes (2) system clock and low power consumption mode
Build your own stock analysis system based on b\s architecture
API data interface for historical data of A-share index
[training Day10] point [enumeration] [bidirectional linked list]
Transport layer protocol parsing -- UDP and TCP
[training Day10] linear [mathematics] [thinking]
Baidu interview question - judge whether a positive integer is to the power of K of 2
A new UI testing method: visual perception test
Unitywebgl project summary (unfinished)
[basic data mining technology] KNN simple clustering
APR learning failure problem location and troubleshooting