当前位置:网站首页>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
边栏推荐
- ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
- Ci framework learning of PHP
- 2022 0726 顾宇佳 学习笔记
- 把Excel转换成CSV/CSV UTF-8
- PNA肽核酸修饰多肽Suc-Tyr-Leu-Val-pNA|Suc-Ala-Pro-Phe-pNA 11
- 强网杯2021 pwn 赛题解析——babypwn
- DNA modified noble metal nanoparticles | DNA modified gold nanoparticles (scientific research level)
- Web configuration software for industrial control is more efficient than configuration software
- Campus news release management system based on SSM
- 硫化镉CdS量子点修饰脱氧核糖核酸DNA|CdS-DNA QDs|近红外CdS量子点偶联DNA规格信息
猜你喜欢

PNA peptide nucleic acid modified peptide suc Tyr Leu Val PNA | suc ala Pro Phe PNA 11

nvidia-smi 各参数意义

(转帖)eureka、consul、nacos的对比2

Reflection on pytorch back propagation

基于SSM实现的校园新闻发布管理系统

The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.

String类的用法

基于SSM图书借阅管理系统

Why can cross entropy loss be used to characterize loss

(转帖)eureka、consul、nacos的对比1
随机推荐
Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.
ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
Pytorch uses data_ Prefetcher improves data reading speed
Talk about multimodality of fire
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
Pytorch model
VScode连接远程服务器开发
Digital image processing Chapter 1 Introduction
Unittest suite and runner
Image super-resolution evaluation index
String类的用法
肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA
Web configuration software for industrial control is more efficient than configuration software
Cyclegan parsing
36 - 新的 Promise 方法:allSettled & any & race
Codeforces Round #804 (Div. 2)(5/5)
Vscode creates golang development environment and debug unit test of golang
[unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature
Why can cross entropy loss be used to characterize loss