Fibonacci

Question

Problem Statement

Find the _N_th number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.
  • The i th number is the sum of i-1 th number and i-2 th number.

The first ten numbers in Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

Example

Given 1, return 0

Given 2, return 1

Given 10, return 34

Note

The _N_th fibonacci number won’t exceed the max value of signed 32-bit integer
in the test cases.

题解

斐波那契数列使用递归极其容易实现,其实使用非递归的方法也很容易,不断向前滚动即可。

C++

  1. class Solution{
  2. public:
  3. /**
  4. * @param n: an integer
  5. * @return an integer f(n)
  6. */
  7. int fibonacci(int n) {
  8. if (n <= 0) return -1;
  9. if (n == 1) return 0;
  10. if (n == 2) return 1;
  11. int fn = 0, fn1 = 0, fn2 = 1;
  12. for (int i = 3; i <= n; i++) {
  13. fn = fn1 + fn2;
  14. fn1 = fn2;
  15. fn2 = fn;
  16. }
  17. return fn;
  18. }
  19. };

Java

  1. class Solution {
  2. /**
  3. * @param n: an integer
  4. * @return an integer f(n)
  5. */
  6. public int fibonacci(int n) {
  7. if (n <= 0) return -1;
  8. if (n == 1) return 0;
  9. if (n == 2) return 1;
  10. int fn = 0, fn1 = 1, fn2 = 0;
  11. for (int i = 3; i <= n; i++) {
  12. fn = fn1 + fn2;
  13. fn2 = fn1;
  14. fn1 = fn;
  15. }
  16. return fn;
  17. }
  18. }

源码分析

  1. corner cases
  2. 初始化 fn, fn1, fn2, 建立地推关系。
  3. 注意 fn, fn2, fn1的递推顺序。

复杂度分析

遍历一次,时间复杂度为 O(n), 使用了两个额外变量,空间复杂度为 O(1).