青蛙跳台阶

package demo5;

import java.util.Scanner;

public class JumpFllorTest {

    public static void main(String[] args) {

        Scanner sc  = new Scanner(System.in);
        while(sc.hasNext()){

            int target = sc.nextInt();
            int res = jumpFloor2(target);
            System.out.println(res);
        }
    }

    // 递归方式 效率低
    public static int jumpFloor1(int target){
        
        if(target <= 0) return 0;
        if(target == 1 || target == 2) return target;
        
        return jumpFloor1(target-1) + jumpFloor1(target-2);
    }

    
    // 非递归方式
    public static int jumpFloor2(int target){
        
        if(target <= 0) return 0;
        if(target == 1 || target == 2) return target;
        
        int pre1 = 1;
        int pre2 = 2;

        for (int i = 3; i <= target; i++) {
            int cur = pre1 + pre2;
            pre1 = pre2;
            pre2 = cur;
        }
        return pre2;
    }
}