当前位置:网站首页>JZ10 斐波那契数列
JZ10 斐波那契数列
2022-07-20 21:38:00 【syc596】
斐波那契数列_牛客题霸_牛客网 (nowcoder.com)
// //fib
// //fib(1)-1
// //fib(2)-1
// //fib(3)-2
// //递归
// public class Solution {
// public int Fibonacci(int n) {
// if(n==0){
// return 0;
// }
// if(n==1){
// return 1;
// }
// if(n==2){
// return 1;
// }
// return Fibonacci(n-1)+Fibonacci(n-2);
// }
// }
// //递归-使用map剪枝
noac
// import java.util.HashMap;
// public class Solution {
// public int Fibonacci(int n) {
// if(n==0){
// return 0;
// }
// if(n==1){
// return 1;
// }
// if(n==2){
// return 1;
// }
// HashMap<Integer,Integer> map=new HashMap<>();
// int first=0;
// if(map.containsKey(n-2)){
// first=map.get(n-2);
// }else{
// first=Fibonacci(n-2);
// map.put(n-2,first);
// }
// int second=0;
// if(map.containsKey(n-1)){
// second=map.get(n-1);
// }else{
// second=Fibonacci(n-1);
// map.put(n-1,second);
// }
// return first+second;
// }
// }
//迭代
public class Solution {
public int Fibonacci(int n) {
if(n==0){
return 0;
}
if(n==1){
return 1;
}
if(n==2){
return 1;
}
int first=1;
int second=1;
int third=0;
while(n>2){
third=first+second;
first=second;
second=third;
n--;
}
return third;
}
}
边栏推荐
- Let me introduce you to the partition automatic management of data warehouse
- Xqlla2.3.2 parsing query
- Tian Wei, architect of Alibaba cloud native application platform: planning, governance and evolution of application architecture
- i2c adapter驅動
- L'ipaylinks, un service d'intégration des paiements transfrontaliers, a remporté le prix 3A Asia de l'actif!
- How can do while call the ODPs SQL written?
- Shape and boundary-aware multi-branch model for semi-supervised medical image segmentation
- Is there any risk in opening a mobile stock account? Is it safe?
- 專注跨境支付一體化服務 iPayLinks榮獲《財資》(The Asset)3A亞洲獎!
- [tensorflow & pytorch] create tensor learning notes
猜你喜欢
Tian Wei, architect of Alibaba cloud native application platform: planning, governance and evolution of application architecture
Swiftui uses spritekit to create rain animation effects (tutorial includes source code)
[kaggle] how to effectively avoid oom (out of memory) and long alchemy process
C | | structure consortium enumeration type
Blueprism tool menu bar user rights function description -rpa Chapter 2
What if the download of Duoyu security browser is blocked? Turn off the method of download blocking
专注跨境支付一体化服务 iPayLinks荣获《财资》(The Asset)3A亚洲奖!
Dama Chapter 4 (data architecture)
What preliminary work does the enterprise need to do to establish its own applet and app?
conda虚拟环境默认路径如何修改
随机推荐
This domestic API artifact tool is too strong Let me give up postman
[example] input text in the input box to output the typewriter effect
C | file operation
Esp8266 nodemcu -- use u8g2 library to light OLED
Translation of UAV intelligent coverage navigation based on DRL in complex geometric environments
Dama Chapter 9 (document and content management)
Database constraint & MySQL advanced query
致测试/开发程序员:爱生活,不加班,苦逼的程序员......
Change dimension style text fill color
Starfish OS: create a new paradigm of the meta universe with reality as the link
Imitate the widget control of VTK, and make the control that draws a rectangle
DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
Three structures of C language
Scanning code for vulnerabilities -- those that need to be rectified
30 times performance improvement -- implementation of MyTT index library based on dolphin DB
Multi thread anti conflict
Dama Chapter 6 (data storage and operation)
Lora base station coverage
ROS from entry to mastery (x) TF coordinate transformation principle, why do you need TF transformation?
Tian Wei, architect of Alibaba cloud native application platform: planning, governance and evolution of application architecture