-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJumpFloorII.java
More file actions
40 lines (38 loc) · 934 Bytes
/
JumpFloorII.java
File metadata and controls
40 lines (38 loc) · 934 Bytes
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
import org.junit.Test;
public class JumpFloorII {
//运行时间:21ms
//占用内存:9412k
// J(n) = J(n-1) + J(n-2) + ... + J(2) + J(1)
/*public int JumpFloorII(int target) {
int res = 0;
if (target == 0 || target == 1) return 1;
if (target == 2) return 2;
for (int i = 0; i < target; i++) {
res += JumpFloorII(i);
}
return res;
}*/
//运行时间:19 ms
//占用内存:9468k
public int JumpFloorII(int target) {
if (target == 1 || target == 0) return 1;
return 1 << (target - 1);
}
@Test
public void test() {
/**
* 1
* 2
* 4
* 8
* 16
* 32
* 64
* 128
* 256
*/
for (int i = 0; i <= 10; i++) {
System.out.println(JumpFloorII(i));
}
}
}