每日一道Leetcode算法——Fibonacci Number——2019.02.01

题干:

中文:

Fibonacci数字,通常表示为F(n),形成一个称为Fibonacci序列的序列,

这样每个数字是前两个数字的总和,从0和1开始。

即, F(0)= 0,F(1)= 1 对于N> 1,F(N)= F(N-1)+ F(N-2)。 给定N,计算F(N)。

Example 1:

Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:

0 ≤ N ≤ 30.

英文:

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

扫描二维码关注公众号,回复: 5825029 查看本文章
F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.

Given N, calculate F(N).

Example 1:

Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:

0 ≤ N ≤ 30.


解题思路:

这道题的主要思想是递归。
从N为2开始,每个数是前两个数的和,所以每次返回值为前两个数的和。
当为0和1时是固定值,作为递归的终止条件。
递归一定要有终止条件,要不然可能会导致栈溢出。
package cn.leetcode.easy;

/**
 * The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
 * such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
 * F(0) = 0,   F(1) = 1
 * F(N) = F(N - 1) + F(N - 2), for N > 1.
 * Given N, calculate F(N).
 *
 * @author kimtian
 * @date 2019-02-01
 * @num 509
 */
public class FibonacciNumber {
    /**
     * 这道题的主要思想是递归,
     * 从N为2开始,每个数是前两个数的和,所以每次返回值为前两个数的和,
     * 当为0和1时是固定值,作为递归的终止条件
     * 递归一定要有终止条件,要不然可能会导致栈溢出
     *
     * @param N
     * @return
     */
    public static int fib(int N) {
        //当为0时,结果为0,作为递归的终止条件
        if (N == 0) {
            return 0;
        }
        //当为1时,结果为1,作为递归的终止条件
        if (N == 1) {
            return 1;
        }
        //其他情况下结果为前两个值之和
        return fib(N - 1) + fib(N - 2);
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        System.out.println(fib(3));
    }
}

猜你喜欢

转载自blog.csdn.net/third_/article/details/86708137