当前位置:网站首页>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;
}
}
核心的思路是怎么让循环建立起来
边栏推荐
猜你喜欢
安装flask
GNNExplainer applied to node classification task
使用百度EasyDL实现智能垃圾箱
Go lang1.18入门精炼教程——第一章:环境搭建
力扣刷题180
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
C# 利用iTextSharp画PDF
随机推荐
带头双向循环链表的增删查改(C语言实现)
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
TCP segment of a reassembled PDU
Introduction to AIOT
报错:flask: TypeError: ‘function‘ object is not iterable
Excel受保护的工作表怎么操作?
Likou Brush Question 180
Xilinx Zynq ZynqMP DNA
VS2019常用快捷键
Unity C# 委托——事件,Action,Func的作用和区别
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
6 states of a thread
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
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
VB.net程序关闭后后台还在与SQL连接
Online tool for sorting multi-line strings
22 high mid term paper topics forecast
sql problem solving statement to create a table
phpstudy install flarum forum