当前位置:网站首页>[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]
[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]
2022-04-23 21:20:00 【Minaldo7】
subject :
Write a function , Input n , Fibonacci, please (Fibonacci) The number of the sequence n term ( namely F(N)). Fibonacci series is defined as follows :
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), among N > 1.
The Fibonacci series is composed of 0 and 1 Start , The Fibonacci number after that is the sum of the two numbers before .
The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.
Example 1:
Input :n = 2
Output :1
Example 2:
Input :n = 5
Output :5
Tips :
0 <= n <= 100
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
With recursion, the timeout will be displayed , So use a loop instead of recursion .
class Solution {
public int fib(int n) {
if(n == 0 || n == 1) return n;
int front = 0, back = 1, fibn =0;
for(int i = 2;i<=n;i++){
fibn = front + back;
if(fibn >= 1000000007)
fibn -= 1000000007;
front = back;
back = fibn;
}
return fibn;
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479570.html
边栏推荐
- 韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
- PHP的Laravel与Composer部署项目时常见问题
- 100天拿下11K,转岗测试的超全学习指南
- South Korea may ban apple and Google from offering commission to developers, the first in the world
- Reentrant function
- Write table of MySQL Foundation (create table)
- Detectron2 usage model
- Express③(使用Express编写接口、跨域有关问题)
- matplotlib. Pyplot partition drawing
- Pytorch selects the first k maximum (minimum) values and their indexes in the data
猜你喜欢
居家第二十三天的午饭
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Is rust more suitable for less experienced programmers?
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
MySQL basic collection
浅谈数据库设计之三大范式
wait、waitpid
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Two Stage Detection
Google 尝试在 Chrome 中使用 Rust
随机推荐
UKFslam
pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
[leetcode refers to the substructure of offer 26. Tree (medium)]
Deno 1.13.2 发布
Some grounded words
How to learn software testing? Self study or training? After reading this article, you will understand
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
Another data analysis artifact: Polaris is really powerful
如何发挥测试策略的指导性作用
Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
presto on spark 支持3.1.3记录
C, print the source program of beautiful bell triangle
Win 11K in 100 days, super complete learning guide for job transfer test
Deep analysis of C language pointer (Part I)
MySQL数据库常识之储存引擎
setInterval、setTimeout、requestAnimationFrame
Selenium displays webdriverwait
Communication between RING3 and ring0
电脑越用越慢怎么办?文件误删除恢复方法
Graph traversal - BFS, DFS