当前位置:网站首页>leetcode 509. Fibonacci Number(斐波那契数字)
leetcode 509. Fibonacci Number(斐波那契数字)
2022-07-07 02:17:00 【蓝羽飞鸟】
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n, calculate F(n).
Example 1:
Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
著名的Fibonacci数字,F(0)=0, F(1)=1,
后面依次是前两个数字的和,求第n个Fibonacci数字。
思路:
DP
可以用一维数组记录下0~n个Fibonacci数字,然后第i个数字就是F(i-2)+F(i-1),
但是因为只用到前两个,所以只需要用两个数字保存就行了。
public int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
int n1 = 0;
int n2 = 1;
for(int i = 2; i <= n; i ++) {
int tmp = n1 + n2;
n1 = n2;
n2 = tmp;
}
return n2;
}
边栏推荐
- "Parse" focalloss to solve the problem of data imbalance
- Array proof during st table preprocessing
- Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
- LM11丨重构K线构建择时交易策略
- Matlab / envi principal component analysis implementation and result analysis
- Can't you really do it when you are 35 years old?
- FPGA课程:JESD204B的应用场景(干货分享)
- Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
- 请问如何查一篇外文文献的DOI号?
- VIM mapping large K
猜你喜欢
Overview of FlexRay communication protocol
请问如何查一篇外文文献的DOI号?
面试中有哪些经典的数据库问题?
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
String (explanation)
MySQL卸载文档-Windows版
JESD204B时钟网络
JMeter function assistant - random value, random string, fixed value random extraction
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
Etcd database source code analysis -- starting from the start function of raftnode
随机推荐
The difference between string constants and string objects when allocating memory
POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells
Handling hardfault in RT thread
Basic DOS commands
Several key steps of software testing, you need to know
Apache ab 压力测试
Cloudcompare point pair selection
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
Can't you really do it when you are 35 years old?
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
Tkinter window selects PCD file and displays point cloud (open3d)
Shared memory for interprocess communication
雷特智能家居龙海祁:从专业调光到全宅智能,20年专注成就专业
对称的二叉树【树的遍历】
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
js装饰器@decorator学习笔记
【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
Jmeter 5.5版本发布说明
CloudCompare-点对选取