左程云老师的矩阵算法
class Solution {
public int fib(int n) {
if(n == 0) return 0;
if(n==1||n==2) return 1;
int [][] base = {{1,1},{1,0}};
int [][] res = matrixPower(base,n-2);
return (res[0][0]+res[1][0])%1000000007;
}
public int[][] matrixPower(int[][] m,int p){
int[][] res = new int[m.length][m[0].length];
for(int i=0;i<res.length;i++){
res[i][i] = 1;
}
int[][] tmp =m;
for(;p!=0;p>>=1){
if((p&1)!=0){
res = muliMatrix(res,tmp);
}
tmp = muliMatrix(tmp,tmp);
}
return res;
}
public int[][] muliMatrix(int[][] m1,int[][] m2){
int[][] res = new int[m1.length][m2[0].length];
for(int i=0;i<m1.length;i++){
for(int j=0;j<m2[0].length;j++){
for(int k=0;k<m2.length;k++){
res[i][j]+=m1[i][k]*m2[k][j];
}
}
}
return res;
}
}