-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinbonacci09.java
More file actions
62 lines (61 loc) · 1.36 KB
/
Finbonacci09.java
File metadata and controls
62 lines (61 loc) · 1.36 KB
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
53
54
55
56
57
58
59
60
61
62
package offer;
/*
* fibonacci����
* 0 1 1 2
*/
public class Finbonacci09 {
/*
* 跳台阶
*/
public int JumpFloor(int n){
int result[] = {0, 1, 2};
if(n < 3){
return result[n];
}
int first = 1;
int two = 2;
int fib = 0;
for (int i = 3; i <= n; i++) {
fib = first + two;
first = two;
two = fib;
}
return fib;
}
/*
* ��̬��̨�� 2^(n-1)
*/
public int JumpFloorII(int target){
int result[] = new int[target + 1];
result[0] = 1;
result[1] = 1;
if(target < 2){
return result[target];
}
int fib = 0;
for (int i = 2; i <= target; i++) {
for (int j = 0; j < i; j++) {
result[i] += result[j];
}
}
return result[target];
}
public int Fibonacci(int n) {
int result[] = {0, 1};
if(n < 2){
return result[n];
}
int first = 0;
int two = 1;
int fib = 0;
for (int i = 2; i <= n; i++) {
fib = first + two;
first = two;
two = fib;
}
return fib;
}
public static void main(String[] args) {
System.out.println(new Finbonacci09().JumpFloor(3));
}
}