当前位置:网站首页>Leetcode skimming -- drip record 016
Leetcode skimming -- drip record 016
2022-07-21 05:01:00 【Robust least squares support vector machine】
16. The finger of the sword Offer 10-I. Fibonacci sequence
requirement
Write a function , Input n , Fibonacci, please (Fibonacci) The number of the sequence n term ( namely F(N)). The Fibonacci series is composed of 0 and 1 Start , The Fibonacci number after that is the sum of the two numbers before . The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.
Ideas
Dynamic programming method :
- State definition : set up dp It's a one-dimensional array , among dp[i] The value of represents Fibonacci number one i A digital
- Transfer equation : dp[i] = dp[i - 1] + dp[i - 2], That is, the definition of the corresponding sequence f(n) = f(n - 1) + f(n - 2)
- The initial state : dp[0] = 0, dp[1] = 1, That is, initialize the first two numbers
- Return value : dp[n], That is, the second of the Fibonacci sequence n A digital
Problem solving
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
int fib(int n) {
if (n == 0) {
return 0; // If you ask f(0) Then return directly 0 , initialization f(0)
}
vector<int> dp(n + 1, 0); // 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 f = s.fib(i);
cout << f << " ";
}
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
边栏推荐
猜你喜欢
自定义类型的深度解析
Leaflet 加载超图发布的投影图层报错 Uncaught No projection definition for code XXXX
乙醇是什么?
Two way LSTM Chinese microblog emotion classification project
Kotlin学习之json数据解析
文末送书|豆瓣9.4分,“hello,world”起源于这本书!
Learn how to choose chart types, and Xiaobai can also play with data analysis
尚医通项目总结
SCS [1] today starts a single-cell journey, describing the past and present lives of single-cell sequencing
[latex] miktex+texstudio installation and configuration of thesis writing environment
随机推荐
DNA 12. Genome wide association analysis visualization of SCI article mapping (GWAS)
FigDraw 12. Correlation matrix of SCI article drawing
A very simple and beautiful login box
The latest upx3.91 supports win64 / PE plus / minus shell
三子棋游戏的实现
尚医通项目总结
Getting started with native threejs
文末送书|豆瓣9.4分,“hello,world”起源于这本书!
selenium框架操作stealth.min.js文件隐藏浏览器指纹特征
Principle and protection of DOM XSS
LeetCode刷题--点滴记录016
Router link opens a new page Jump and a tag to prevent default jump and various attributes
一个极简美的登录框
C language file operation management (Part 2)
À propos des outils d'édition XML
[latex] miktex+texstudio installation and configuration of thesis writing environment
输入10个数, 使其所有的数向后移m个位置
Isn't it too much to play Gobang in idea?
人人代码生成器--简化你的开发
Keil编译下载报错:No Algorithm found for: 08000000H - 08001233H解决办法