当前位置:网站首页>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;
}
}
核心的思路是怎么让循环建立起来
边栏推荐
- 数据库中间件-jdbi
- 思维方法 解决问题的能力
- Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
- [R language] Extract all files under a folder to a specific folder
- VS2019常用快捷键
- Altium designer软件常用最全封装库,包含原理图库、PCB库和3D模型库
- 逆向工程
- Introduction and use of BeautifulSoup4
- 输入框最前面添加放大镜&&background-image和background-color冲突问题
- sql problem solving statement to create a table
猜你喜欢
输入框最前面添加放大镜&&background-image和background-color冲突问题
带头双向循环链表的增删查改(C语言实现)
中英文说明书丨CalBioreagents 醛固酮单克隆抗体
安装flask
sql problem solving statement to create a table
缓存技术使用
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
[email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop"/>
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
力扣刷题180
工控设备的系统如何进行加固
随机推荐
Introduction to AIOT
中英文说明书丨TRC D-阿卓糖(D-Altrose)
INSTALL_RPATH and BUILD_RPATH problem in CMake
【R语言】把文件夹下的所有文件提取到特定文件夹
DevNet: Deviation Aware Networkfor Lane Detection
阿里巴巴官方技术号
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
Output method of list string print(*a) print(““.join(str(c) for c in a) )
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
BeautifulSoup4的介绍与使用
Web APIs BOM- 操作浏览器:本地存储
[R language] Extract all files under a folder to a specific folder
C language implements sequential stack and chain queue
idea中PlantUML插件使用
golang xml 处理动态属性
数据库中间件-jdbi
e-learning summary
线程的6种状态
思维方法 解决问题的能力