当前位置:网站首页>P6阿里机试题之2020 斐波那契数
P6阿里机试题之2020 斐波那契数
2022-08-09 06:29:00 【史上最强的弟子】
斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
给定 N,计算 F(N)。
示例 1:
输入:2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1.
示例 2:
输入:3
输出:2
解释:F(3) = F(2) + F(1) = 1 + 1 = 2.
示例 3:
输入:4
输出:3
解释:F(4) = F(3) + F(2) = 2 + 1 = 3.
class Solution {
public int fib(int N) {
int fn2 = 0;
if(N == 0) return fn2;
int fn1 = 1;
if(N == 1) return fn1;
int returnFn = 0;
for(int i =2;i<=N;i++){
returnFn = fn2 + fn1;
fn2 = fn1;
fn1 = returnFn;
}
return returnFn;
}
}核心的思路是怎么让循环建立起来
边栏推荐
- jdepend
- 中英文说明书丨CalBioreagents 醛固酮单克隆抗体
- 阿里巴巴官方技术号
- C语言实现顺序栈和链队列
- Program Performance Analysis - Complexity Analysis
- 2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
- mongo+ycsb性能测试及线程数分析
- 像天才一样思考:如何培养自己的创造力?
- 按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程
- 数据库中间件-jdbi
猜你喜欢
随机推荐
Unity Gobang Game Design and Simple AI(3)
mmdetection源码解析--ResNet18
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
The singleton pattern
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
kubernetes security
[R language] Normalize and organize files into folders of various file types
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
【R语言】交互作用 测试数据
简单工厂模式
idea中PlantUML插件使用
中英文说明书丨CalBioreagents 醛固酮单克隆抗体
05 多线程与高并发 - ThreadPoolExecutor 源码解析
flask创建数据库失败未报错
XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
思维方法 解决问题的能力
Use baidu EasyDL intelligent bin
网络学习总结
2022.8.8DAY628







