forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.java
52 lines (47 loc) · 1.05 KB
/
Fibonacci.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.haobin.offer;
/**
* @author: HaoBin
* @create: 2019/9/23 10:31
* @description: 斐波那契数列
* 实现斐波那契数列
**/
public class Fibonacci {
/**
* 思路:
* 斐波那契数列:
* f(n) = f(n-1) + f(n-2) (n>1)
* = 0 (n=0)
* = 1 (n=1)
*
* 可以看出用递归是最简单的
*/
public static void main(String[] args) {
System.out.println(recursion(5));
System.out.println(fibonacci(5));
}
/**
* 递归实现
*/
public static int recursion(int n) {
if (n <= 2) {
return 1;
}
return recursion(n-1) + recursion(n-2);
}
/**
* 动态规划实现
*/
public static int fibonacci(int n) {
if (n <= 2) {
return 1;
}
int pre1 = 1, pre2 =1;
int fib = 0;
for (int i = 2; i < n; i++) {
fib = pre1 + pre2;
pre1 = pre2;
pre2 = fib;
}
return fib;
}
}