当前位置:网站首页>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;
}
}
核心的思路是怎么让循环建立起来
边栏推荐
- A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
- 电学知识的疑问
- 抗菌药物丨Toronto Research Chemicals 天冬酰胺D
- String.toLowerCase(Locale.ROOT)
- 【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
- [GO], arrays and slices
- crc calculation
- Silently start over, the first page is also a new page
- Unity 五子棋游戏设计和简单AI(3)
- golang zip aes base64
猜你喜欢
PDF不能打印和复制的问题如何解决?
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Unity五子棋游戏设计 和简单AI实现(1)
Fragments
CMake中INSTALL_RPATH与BUILD_RPATH问题
Error jinja2.exceptions.UndefinedError: 'form' is undefined
GNNExplainer应用于节点分类任务
什么是excel文件保护
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
idea中PlantUML插件使用
随机推荐
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
Unity Gobang Game Design and Simple AI (2)
Can Jincang Database Set Transaction Automatic Commit?
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
22 high mid term paper topics forecast
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
After the VB.net program is closed, the background is still connected to SQL
GNNExplainer应用于节点分类任务
05 多线程与高并发 - ThreadPoolExecutor 源码解析
db.sqlite3 has no "as Data Source" workaround
mongo+ycsb性能测试及线程数分析
[GO], arrays and slices
常用Oracle命令
TCP segment of a reassembled PDU
el-table缓存数据
flask创建数据库失败未报错
【R语言】把文件夹下的所有文件提取到特定文件夹
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
Xilinx Zynq ZynqMP DNA
Unity 五子棋游戏设计和简单AI(2)