【LeetCode】Day49-斐波那契数

题目1

509. 斐波那契数【简单】

题解

动态规划

class Solution {
    public int fib(int n) {
        if(n<2)
            return n;
        int first=0,second=1,third=0;
        for(int i=2;i<=n;i++){
            third=first+second;
            first=second;
            second=third;
        }
        return third;
    }
}

时间复杂度: O ( n ) O(n) O(n)

空间复杂度: O ( 1 ) O(1) O(1)

矩阵快速幂

在这里插入图片描述
关于java中的位运算,这篇文章说的比较清楚【Java位运算】n&1和n>>1含义

矩阵快速幂模板:

public int[][] pow(int[][] a, int n) {
    int[][] ret = {{1, 0}, {0, 1}};//单位矩阵
    while (n > 0) {
        if ((n & 1) == 1) {
            ret = multiply(ret, a);
        }
        n >>= 1;
        a = multiply(a, a);
    }
    return ret;
}

此题代码:

class Solution {
    public int fib(int n) {
        if(n<2)
            return n;
        int[][] a={{1,1},{1,0}};
        int[][] res=pow(a,n-1);
        return res[0][0];
    }
    //矩阵快速幂
    public int[][] pow(int[][] a,int n){
        int[][] ret={{1,0},{0,1}};//单位矩阵
        while(n>0){
            //奇数
            if((n&1)==1)
                ret=multiply(ret,a);
            a=multiply(a,a);
            n>>=1;
        }
        return ret;
    }
    //矩阵乘法
    public int[][] multiply(int[][] a,int[][] b){
        int[][] c=new int[2][2];
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j];
            }
        }
        return c;
    }
}

时间复杂度: O ( l o g n ) O(logn) O(logn)

空间复杂度: O ( 1 ) O(1) O(1)

题目2

1137. 第 N 个泰波那契数【简单】

题解

动态规划

class Solution {
    public int tribonacci(int n) {
        if(n<3)
            return n<2?n:1;
        int first=0,second=1,third=1,forth=0;
        for(int i=3;i<=n;i++){
            forth=first+second+third;
            first=second;
            second=third;
            third=forth;
        }
        return forth;
    }
}

时间复杂度: O ( n ) O(n) O(n)

空间复杂度: O ( 1 ) O(1) O(1)

矩阵快速幂

在这里插入图片描述

class Solution {
    public int tribonacci(int n) {
        if(n<3)
            return n<2?n:1;
        int[][] a={{1,1,1},{1,0,0},{0,1,0}};
        int[][] res=mi(a,n);
        return res[0][2];
    }
    //矩阵快速幂
    public int[][] mi(int[][] a,int n){
        int[][] ret={{1,0,0},{0,1,0},{0,0,1}};
        while(n>0){
            if((n&1)==1)
                ret=multiply(ret,a);
            a=multiply(a,a);
            n>>=1;
        }
        return ret;
    }
    //矩阵乘法
    public int[][] multiply(int[][] a,int[][] b){
        int[][] c=new int[3][3];
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j];
            }
        }
        return c;
    }
}

时间复杂度: O ( l o g n ) O(logn) O(logn)

空间复杂度: O ( 1 ) O(1) O(1)

p.s. 听师兄的建议,开始了动态规划的每日训练~每天两三道,动态规划真的让人头疼,希望能通过21天的训练拿下!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值