当前位置:网站首页>Lintcode 366:fibonacci Fibonacci sequence
Lintcode 366:fibonacci Fibonacci sequence
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
Find the Nth number in Fibonacci sequence.
A Fibonacci sequence is defined as follow:
- The first two numbers are 0 and 1.
- The i th number is the sum of i-1 th number and i-2 th number.
The first ten numbers in Fibonacci sequence is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
Example
Given 1, return 0
Given 2, return 1
This problem belongs to dynamic programming , There are two ways , recursive ( Overtime ) And non recursive .
recursive :
class Solution {
/**
* @param n: an integer
* @return an integer f(n)
*/
public int fibonacci(int n) {
// write your code here
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fibonacci(n-1) + fibonacci(n-2);
}
}Non recursive :
self:
public class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public static int[] f = new int[1000];
public int fibonacci(int n) {
// write your code here
for(int i = 0;i < f.length; ++i){
f[i] = -1;
}
return search(n);
}
public int search(int n){
if(f[n] >= 0)
return f[n];
else if(n == 1)
f[n] = 0;
else if(n == 2)
f[n] = 1;
else
f[n] = search(n - 1) + search(n - 2);
return f[n];
}
}reference:(better)
class Solution {
/**
* @param n: an integer
* @return an integer f(n)
*/
public int fibonacci(int n) {
// write your code here
if(n==1)
return 0;
if(n==2)
return 1;
int f0 = 0;
int f1 = 1;
int i = 3;
int f = 0;
while(i<=n){
f = f0 + f1;
f0 = f1;
f1 = f;
i++;
}
return f;
}
}
边栏推荐
- Getting to know Alibaba cloud environment construction for the first time: unable to connect remotely, and having been in the pit: the server Ping fails, FTP is built, the server builds the database,
- find()(名字太多人用我就加字)
- Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology
- Detailed explanation of metauniverse public chain caduceus: a creative platform specially built for metauniverse application
- 性能测试实施规范指南
- 深入性能测试数据分析
- Bladex - a well-designed microservice architecture
- Flask template injection
- How to configure multiple SSH keys for novices (easy to understand hand-in-hand teaching)
- Zabbix6.0通过iDRAC,IMM2监控DELL,IBM服务器硬件
猜你喜欢

全链路压测

Swagger -- the most popular API framework in the world

服务器知识(详情)

CTFHub----RCE

PowerStor500T报错0x01806803

How to add software shortcuts to the right mouse button list
![[solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti](/img/a8/2daa2c0d834f1986c8421bf5138c7e.png)
[solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti

Detailed explanation of caduceus project of metauniverse public chain (I): project concept and technical framework of caduceus metaverse protocol

InnoDB, MySQL structure, and the difference between the three kinds of deletion

Network layer protocol and IP packet format (detailed)
随机推荐
[tools] unity quickly starts to make the artifact tilemap of 2D and 2.5D games
Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology
Experience in using flow playback tool Gor
Server knowledge (details)
解决WIN10连接共享打印机出现0x00000709的错误
MySQL备份和恢复
正则、扩展表达式,sed文本处理器与awk工具、用脚本改IP地址
status 500 reading AftersaleService#getAftersaleList(Long)+com.sun.proxy.$Proxy214.getAftersaleList
Shell脚本case分支语句、扒匿名登录FTP的max地址
ctfhub--ssrf
Array transformer blocking idea
BeanShell script gets the current time
MDK Keil/ARM printf 用法说明
Subnet division (see details)
Shortest circuit / secondary short circuit /k short circuit
Firewalld 防火墙
简单的用例编写规范
网络一般知识(详)
GoReplay
安装软件提示无法定位程序输入点AddDllDirectory于动态链接库Kernel32.dll上(文末有下载地址)