当前位置:网站首页>JZ10 斐波那契数列
JZ10 斐波那契数列
2022-08-02 15:35:00 【syc596】
JZ10 斐波那契数列
斐波那契数列_牛客题霸_牛客网 (nowcoder.com)
// //递归
// 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);
// }
// }
// //迭代
// 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;
// for(int i=3;i<=n;i++){
// third=first+second;
// first=second;
// second=third;
// }
// return third;
// }
// }
//动规
public class Solution {
public int Fibonacci(int n) {
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
int[] dp=new int[n+1];
dp[0]=0;
dp[1]=1;
dp[2]=1;
for(int i=3;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}
}边栏推荐
猜你喜欢
随机推荐
SIGIR'22 推荐系统论文之序列推荐(长文)篇
阿里巴巴《MySQL成长手册》精简版
Anti-shake throttling (continue to update later)
博世「求援」,毫米波雷达重构
vim的高级用法配置
为什么我不再推荐枚举策略模式?
DevOps开发工具对比
【Transformer专题】一、Attention is All You Need(Transformer)
Go-6-常用命令-go包管理问题-两个路径-GO111MODULE
不平衡之钥: 重采样法何其多
WWW'22 推荐系统论文之多任务与对比学习篇
ACL/NAACL'22 推荐系统论文梳理
Linux系统中mysql数据库的基本管理
VLAN实验
暴力破解美团最新JVM面试题:无限执行
“绿色低碳+数字孪生“双轮驱动,解码油气管道站升级难点
机械臂速成小指南(十四):多项式插值轨迹规划
类的比较大小(Comparable -> compareTo(类自己实现接口),Comparator -> compare(新建一个类作为比较器))
Apache的管理及web优化
无线振弦采集仪远程修改参数方式








