跳跃训练 (跳跃训练)

 

思路:动规

// @Title: 跳跃训练 (跳跃训练)
// @Author: qisiii
// @Date: 2022-02-19 19:41:37
// @Runtime: 0 ms
// @Memory: 38.2 MB
// @comment: 动规
// @flag: BLUE
class Solution {
    public int numWays(int n) {
        if(n<=2){
            return n>0?n:1;
        }
        int[] v=new int[n+1];
        v[0]=1;v[1]=1;v[2]=2;
        for(int i=3;i<=n;i++){
            v[i]=(v[i-1]+v[i-2])%1000000007;
        }
        return v[n];
    }
}

+++ title = “跳跃训练 (跳跃训练)” draft = false +++

思路:可优化空间

// @Title: 跳跃训练 (跳跃训练)
// @Author: qisiii
// @Date: 2022-02-19 19:52:08
// @Runtime: 0 ms
// @Memory: 38.2 MB
// @comment: 可优化空间
// @flag: ORANGE
class Solution {
    public int numWays(int n) {
        int a=1;int b=1,sum;
        for(int i=2;i<=n;i++){
            sum=(a+b)%1000000007;
            b=a;
            a=sum;
        }
        return a;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18