当前位置:网站首页>leetcode 509. Fibonacci number
leetcode 509. Fibonacci number
2022-07-07 07:06:00 【Blue feather birds】
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.
The famous Fibonacci Numbers ,F(0)=0, F(1)=1,
Followed by the sum of the first two numbers , Please n individual Fibonacci Numbers .
Ideas :
DP
It can be recorded with a one-dimensional array 0~n individual Fibonacci Numbers , Then the first i One number is F(i-2)+F(i-1),
But because only the first two are used , So just save it with two numbers .
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;
}
边栏推荐
- MYSQL----导入导出&视图&索引&执行计划
- Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
- 算法---比特位计数(Kotlin)
- Networkx绘图和常用库函数坐标绘图
- 非父子组件的通信
- Multidisciplinary integration
- Redhat5 installing vmware tools under virtual machine
- 2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
- from .onnxruntime_pybind11_state import * # noqa ddddocr运行报错
- 分布式id解决方案
猜你喜欢

大咖云集|NextArch基金会云开发Meetup来啦

Brand · consultation standardization

一文带你了解静态路由的特点、目的及配置基本功能示例

Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données

多个kubernetes集群如何实现共享同一个存储

$refs:组件中获取元素对象或者子组件实例:
![Stack and queue-p78-8 [2011 unified examination true question]](/img/df/72ba22f1953551943494d661a56a3b.jpg)
Stack and queue-p78-8 [2011 unified examination true question]

Graduation design game mall

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案

MOS管参数μCox得到的一种方法
随机推荐
Jetpack compose is much more than a UI framework~
【NOI模拟赛】区域划分(结论,构造)
剑指offer-高质量的代码
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
Basic introduction of JWT
多个kubernetes集群如何实现共享同一个存储
Stack and queue-p79-10 [2014 unified examination real question]
Initial experience of addresssanitizer Technology
Please answer the questions about database data transfer
分布式id解决方案
品牌电商如何逆势增长?在这里预见未来!
ANR 原理及实践
toRefs API 与 toRef Api
How to install swoole under window
What books can greatly improve programming ideas and abilities?
How to share the same storage among multiple kubernetes clusters
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
【JDBC以及内部类的讲解】
from . onnxruntime_ pybind11_ State Import * noqa ddddocr operation error
Anr principle and Practice