我先强调一下
请侧重注意时间复杂度,不要在意语句的作用
先上一下斐波那契的代码(解释在注释里面)
跟语言没关系,都差不多啦,大家想看别的语言的解释
没学过Java也能看,只是它在第一个,我就直接复制了(真的)
先上只看时间复杂度的句子
class Solution {
static final int MOD = 1000000007;
public int fib(int n) {
if (n < 2) {
return n;
}
int[][] q = {
{1, 1}, {1, 0}};
int[][] res = pow(q, n - 1);//pow的时间复杂度为对数级,pow函数里面的注释解释了
// 到这里应该清楚对数级是可以算的了吧
return res[0][0];
}
// pow时间复杂度为lgn级别
public int[][] pow(int[][] a, int n) {
int[][] ret