当前位置:网站首页>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];
}
}
边栏推荐
猜你喜欢
随机推荐
MySQL-3-密码破解
类的比较大小(Comparable -> compareTo(类自己实现接口),Comparator -> compare(新建一个类作为比较器))
多商户商城系统功能拆解20讲-平台端分销概况
系统存储的基本管理--挂载,分区,用户配额
数仓:金融级数仓架构转型的最佳实践(下篇)
DevOps开发工具对比
WWW'22 推荐系统论文之序列推荐篇
做好私域流量!全民拼购就可以了。
机械臂速成小指南(十七):直线规划
Advanced usage of vim configuration
微信小程序:Framework inner error FLOW_CREATE_NODE
不平衡问题: 深度神经网络训练之殇
Linux系统中mysql数据库的基本管理
2022年值得尝试的7个MQTT客户端工具
CWE4.8:2022年危害最大的25种软件安全问题
暴力破解美团最新JVM面试题:无限执行
NC231 只出现一次的数字
“如何写好一篇学术论文?”这大概是最详实的一则攻略了!
Brute-force cracking of the latest JVM interview questions of Meituan: unlimited execution
RecSys'22 推荐系统论文梳理