-
Notifications
You must be signed in to change notification settings - Fork 1
/
JumpFloor.java
47 lines (44 loc) · 1015 Bytes
/
JumpFloor.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
import org.junit.Test;
public class JumpFloor {
//运行时间:359ms
//占用内存:9300k
/*public int JumpFloor(int target) {
if (target == 1) return 1;
if (target == 2) return 2;
return JumpFloor(target - 1) + JumpFloor(target - 2);
}*/
//运行时间:11ms
//占用内存:9176k
public int JumpFloor(int target) {
int res = 0;
int f0 = 1;
int f1 = 2;
if (target == 1) res = f0;
if (target == 2) res = f1;
while (target > 2) {
target--;
res = f0 + f1;
f0 = f1;
f1 = res;
}
return res;
}
/**
* 1 : 1
* 2 : 2
* 3 : 3
* 4 : 5
* 5 : 8
* 6 : 13
* 7 : 21
* 8 : 34
* 9 : 55
* 10 : 89
*/
@Test
public void test() {
for (int i = 1; i < 10; i++) {
System.out.println(i + " : " + JumpFloor(i));
}
}
}