当前位置:网站首页>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;
}
}
核心的思路是怎么让循环建立起来
边栏推荐
- Output method of list string print(*a) print(““.join(str(c) for c in a) )
- Silently start over, the first page is also a new page
- Xilinx Zynq ZynqMP DNA
- 【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
- IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
- install flask
- [R language] interaction test data
- Online tool for sorting multi-line strings
- 详解C语言中的wait()函数和waitpid()函数
- Invalid argument(s) appears when redis runs lua script
猜你喜欢
线程的6种状态
Use baidu EasyDL intelligent bin
缓存技术使用
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
6 states of a thread
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
Xilinx Zynq ZynqMP DNA
static静态关键字和继承
install flask
随机推荐
Import the pycharm environment package into another environment
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
ZIP压缩包文件删除密码的方法
什么是excel文件保护
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
普罗米修斯原理及节点发布
Deep Learning - Principles of Neural Networks 2
IQ Products CMV Brite Turbo试剂盒的原理
Qt learning (3) - Qt module
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
mmdetection源码解析--ResNet18
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
VS2019 common shortcut keys
深度学习-神经网络原理2
Regular Expression - Determine if a string matches the "AABB" pattern
Altium designer软件常用最全封装库,包含原理图库、PCB库和3D模型库
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
Fragments
中英文说明书丨CalBioreagents ACTH N端单克隆抗体
输入框最前面添加放大镜&&background-image和background-color冲突问题