剑指Offer:递归和循环

斐波那契数列

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。

n<=39

package jianzhioffer;

import java.util.Scanner;

/**
 * 剑指Offer:斐波那契数列
 */
public class FeiBoNaQie {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int result = Fibonacci(n);
            System.out.println(result);
        }
        sc.close();
    }

    private static int Fibonacci(int n) {
        int[] result = new int[]{0, 1};
        if (n<2)
            return result[n];
        int fibOne =1;
        int fibTwo = 0;
        int fibN = 0;
        for (int i = 2 ; i <=n ; ++i) {
            fibN = fibOne+fibTwo;
            fibTwo = fibOne;
            fibOne = fibN;
        }
        return fibN;
    }
}

跳台阶

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

我们把n级台阶时的跳法看成n的函数,记为f(n)。当n>2时,第一次跳的时候就有两种不同的选择:一是第一次只跳1级,此时的跳法数目等于后面剩下的n-1级台阶的跳法数目,即为f(n-1);二是第一次跳2级,此时的跳法数目等于后面上下的n-2级台阶的跳法数目。即为f(n-2)。因此,n级台阶的不同跳法的总数为f(n)=f(n-1)+f(n-2)。即斐波那契数列。

package jianzhioffer;

import java.util.Scanner;

public class QingWa {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int result = JumpFloor(n);
            System.out.println(result);
        }
        sc.close();
    }

    private static int JumpFloor(int target) {
        int[] result = new int[]{0, 1, 2};
        if (target <= 2)
            return result[target];
        int one = 1;
        int two = 2;
        int N = 0;
        for (int i = 2; i < target; i++) {
            N = one + two;
            one = two;
            two = N;
        }
        return N;
    }
}

变态跳台阶

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

分析如下:

f(1) = 1

f(2) = f(2-1) + f(2-2)         //f(2-2) 表示2阶一次跳2阶的次数。

f(3) = f(3-1) + f(3-2) + f(3-3) 

...

f(n) = f(n-1) + f(n-2) + f(n-3) + ... + f(n-(n-1)) + f(n-n) 

说明: 

1)这里的f(n) 代表的是n个台阶有一次1,2,...n阶的 跳法数。

2)n = 1时,只有1种跳法,f(1) = 1

3) n = 2时,会有两个跳得方式,一次1阶或者2阶,这回归到了问题(1) ,f(2) = f(2-1) + f(2-2) 

4) n = 3时,会有三种跳得方式,1阶、2阶、3阶,

    那么就是第一次跳出1阶后面剩下:f(3-1);第一次跳出2阶,剩下f(3-2);第一次3阶,那么剩下f(3-3)

    因此结论是f(3) = f(3-1)+f(3-2)+f(3-3)

5) n = n时,会有n中跳的方式,1阶、2阶...n阶,得出结论:

    f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) => f(0) + f(1) + f(2) + f(3) + ... + f(n-1)

6) 由以上已经是一种结论,但是为了简单,我们可以继续简化:

    f(n-1) = f(0) + f(1)+f(2)+f(3) + ... + f((n-1)-1) = f(0) + f(1) + f(2) + f(3) + ... + f(n-2)

    f(n) = f(0) + f(1) + f(2) + f(3) + ... + f(n-2) + f(n-1) = f(n-1) + f(n-1)

    可以得出:

    f(n) = 2*f(n-1)

    

7) 得出最终结论,在n阶台阶,一次有1、2、...n阶的跳的方式时,总得跳法为:

              | 1       ,(n=0 ) 

f(n) =     | 1       ,(n=1 )

              | 2*f(n-1),(n>=2)

使用递归的思想代码如下:

1

2

3

4

5

6

7

8

9

10

11

public class Solution {

    public int JumpFloorII(int target) {

        if (target <= 0) {

            return -1;

        } else if (target == 1) {

            return 1;

        } else {

            return 2 * JumpFloorII(target - 1);

        }

    }

}

经过再次分析,我们可知,f(n)=2*f(n-1)=2*(2*f(n-2))=2*(2*(2*(f(n-3))))=......2^(n-1),此时计算2的幂的方法是使用位运算,即将1左移n位。

package jianzhioffer;

import java.util.Scanner;

public class JumpFloorII {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int result = JumpFloorN(n);
            System.out.println(result);
        }
        sc.close();
    }
    /*
    使用递归的思想
     */
//    private static int JumpFloorN(int target) {
//        if (target <= 0) {
//            return -1;
//        } else if (target == 1) {
//            return 1;
//        } else {
//            return 2 * JumpFloorN(target - 1);
//        }
//    }
    /*
    经过分析后,f(n)=2^(n-1)
     */
    private static int JumpFloorN(int target) {
        int result = 1;
        return result<<(target-1);
    }
}

猜你喜欢

转载自blog.csdn.net/ARPOSPF/article/details/81227325
今日推荐