当前位置:网站首页>[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
边栏推荐
- go slice
- presto on spark 支持3.1.3记录
- opencv应用——以图拼图
- Alibaba cloud responded to the disclosure of user registration information
- Unit function expansion
- Arm architecture assembly instructions, registers and some problems
- Google tries to use rust in Chrome
- Tensorflow realizes gradient accumulation, and then returns
- Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
- Solve importerror: cannot import name 'imread' from 'SciPy misc‘
猜你喜欢
wait、waitpid
Linux中,MySQL的常用命令
[leetcode refers to offer 27. Image of binary tree (simple)]
Express ③ (use express to write interface and cross domain related issues)
Zhongchuang storage | how to choose a useful distributed storage cloud disk
[leetcode refers to the substructure of offer 26. Tree (medium)]
MySQL基础合集
What if Jenkins forgot his password
Thinkphp5 + data large screen display effect
PHP的Laravel与Composer部署项目时常见问题
随机推荐
Recommended usage scenarios and production tools for common 60 types of charts
Fastdfs思维导图
Common commands of MySQL in Linux
Leetcode-279-complete square number
Tencent cloud has two sides in an hour, which is almost as terrible as one side..
C, print the source program of beautiful bell triangle
pikachuxss如何获取cookie靶场,返回首页总是失败
Common problems in deploying projects with laravel and composer for PHP
Question brushing plan -- backtracking method (I)
MySQL basic collection
Addition, deletion, modification and query of advanced MySQL data (DML)
Communication between RING3 and ring0
Express③(使用Express编写接口、跨域有关问题)
GSI-ECM工程建设管理数字化平台
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Express ③ (use express to write interface and cross domain related issues)
How to play the guiding role of testing strategy
3-5 obtaining cookies through XSS and the use of XSS background management system
go array
又一款数据分析神器:Polars 真的很强大