当前位置:网站首页>Algorithm -- Fibonacci sequence (kotlin)
Algorithm -- Fibonacci sequence (kotlin)
2022-07-27 07:14:00 【Xiaomi technology Android research and development caoxinyu】
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/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 .
resolvent
fun fib(n: Int): Int {
if (n < 2) {
return n
}
var pre_pre = 0
var pre = 1
var result = 0
for (i in 2..n) {
result = (pre + pre_pre) % 1000000007
pre_pre = pre
pre = result
// println("$pre $pre_pre")
}
return result
}
summary
1. I still haven't thought clearly here Adding numbers that are too large may cause overflow int The range is negative
So be clear
2. Fibonacci series is really the originator of dynamic programming
边栏推荐
- Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces
- 线性表 -- 栈和队列
- Brief introduction of simulation model
- Reflection on pytorch back propagation
- PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA
- (转帖)eureka、consul、nacos的对比2
- PNA肽核酸修饰多肽Suc-Tyr-Leu-Val-pNA|Suc-Ala-Pro-Phe-pNA 11
- DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)
- Dsgan degenerate network
- pre-commit install 时 CalledProcessError
猜你喜欢

A Competitive Swarm Optimizer for Large Scale Optimization

Express framework

基于SSM图书借阅管理系统

ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)

VIVO应用市场APP上架总结

Derivative, partial derivative and gradient

DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)

DNA coupled PbSe quantum dots | near infrared lead selenide PbSe quantum dots modified DNA | PbSe DNA QDs

Relevant principles of MySQL index optimization

Music website management system based on SSM
随机推荐
Brief introduction of simulation model
Automatically generate UML sequence diagram according to text (draw.io format)
(转帖)eureka、consul、nacos的对比1
Cyclegan parsing
Jest single test style problem [identity obj proxy] NPM package
ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)
指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
Bert and RESNET can also be trained on mobile phones?!
Watermelon book chapter 3 - linear model learning notes
String类的用法
"Weilai Cup" 2022 Niuke summer multi school training camp 1
newest! SASAC releases new measures for digital transformation of state-owned enterprises
Gbase 8C core technology
MySQL query operation index optimization practice
Day012 application of one-dimensional array
Derivative, partial derivative and gradient
Watermelon book learning Chapter 5 --- neural network
VIM editor deletes all file contents
R2live code learning record (3): radar feature extraction
基于SSM图书借阅管理系统