当前位置:网站首页>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;
}
边栏推荐
- C language sorting (to be updated)
- 骑士战胜魔王(背包&dp)
- 2022 Android interview essential knowledge points, a comprehensive summary
- 软件测试到了35岁,真的就干不动了吗?
- 面试中有哪些经典的数据库问题?
- LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
- 如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
- 缓存在高并发场景下的常见问题
- 直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级
- Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
猜你喜欢

LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标

字符串常量与字符串对象分配内存时的区别

BindingException 异常(报错)处理

软件测试到了35岁,真的就干不动了吗?

Test the foundation of development, and teach you to prepare for a fully functional web platform environment

POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...

Markdown displays pictures side by side

博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生

PostgreSQL database timescaledb function time_ bucket_ Gapfill() error resolution and license replacement
![Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]](/img/13/096857158c9f977f8677f7cd0f9d4b.png)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
随机推荐
开发者别错过!飞桨黑客马拉松第三期链桨赛道报名开启
哈趣投影黑马之姿,仅用半年强势突围千元投影仪市场!
dolphinscheduler3. X local startup
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
c面试 加密程序:由键盘输入明文,通过加密程序转换成密文并输出到屏幕上。
精准时空行程流调系统—基于UWB超高精度定位系统
Open the blue screen after VMware installation
string(讲解)
[FPGA] EEPROM based on I2C
dolphinscheduler3.x本地启动
如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
Niuke Xiaobai monthly race 52 E. sum logarithms in groups (two points & inclusion and exclusion)
二十岁的我4面拿到字节跳动offer,至今不敢相信
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
JESD204B时钟网络
安装VMmare时候提示hyper-v / device defender 侧通道安全性
Postgresql中procedure支持事务语法(实例&分析)
偏执的非合格公司