怎样自己做网络推广网站,广州天河网站开发公司,北京建设厅网站首页,有那个网站可以做报名链接的问题描述 求Fibonacci数列的第n项。Fibonacci数列为1,1,2,3,5,... 解决思路 (1) 递归#xff0c;指数级时间复杂度#xff1b; (2) 循环#xff0c;O(n)时间复杂度#xff1b; (3) 矩阵乘法#xff0c;O(logn)时间复杂度#xff1b; (4) 公式法#xff0c;O(1)时间复杂度…问题描述 求Fibonacci数列的第n项。Fibonacci数列为1,1,2,3,5,... 解决思路 (1) 递归指数级时间复杂度 (2) 循环O(n)时间复杂度 (3) 矩阵乘法O(logn)时间复杂度 (4) 公式法O(1)时间复杂度。 程序 public class Fibonacci {public int getNthElemRec(int n) {if (n 0) {return -1;}if (n 2) {return 1;}return getNthElemRec(n - 1) getNthElemRec(n - 2);}public int getNthElemNoRec(int n) {if (n 0) {return -1;}if (n 2) {return 1;}int a 1, b 1;int res 0;while (n 3) {res a b;a b;b res;--n;}return res;}
}转载于:https://www.cnblogs.com/harrygogo/p/4623649.html