-
Notifications
You must be signed in to change notification settings - Fork 6
/
ClimbingStairs.java
73 lines (73 loc) · 1.8 KB
/
ClimbingStairs.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.ziheng.recursion.dynamicprogramming.leetcode;
/**
* LeetCode 70. Climbing Stairs
* https://leetcode.com/problems/climbing-stairs/
*/
public class ClimbingStairs {
/**
* 跳台阶 - 自底向上动态规划
*
* @param n
* @return int
*/
public int climbStairsWithBottomToTop(int n) {
if (n < 0) {
return 0;
}
int[] memory = new int[n + 1];
for (int i = 0; i <= memory.length; i++) {
memory[i] = -1;
}
memory[0] = 1;
memory[1] = 1;
for (int i = 2; i < n; i++) {
memory[i] = memory[i - 1] + memory[i - 2];
}
return memory[n];
}
/**
* 跳台阶 - 自顶向下记忆化搜索
*
* @param n
* @return int
*/
public int climbStairsWithTopToBottom(int n) {
if (n < 0) {
return 0;
}
int[] memory = new int[n + 1];
for (int i = 0; i < memory.length; i++) {
memory[i] = -1;
}
return climbStairsWithTopToBottom0(n, memory);
}
private int climbStairsWithTopToBottom0(int n, int[] memory) {
if (n == 0 || n == 1) {
return 1;
}
if (memory[n] == -1) {
memory[n] = climbStairsWithTopToBottom0(n - 1, memory)
+ climbStairsWithTopToBottom0(n - 2, memory);
}
return memory[n];
}
/**
* 跳台阶 - 标准递归解法(Time Limit Exceeded)
*
* @param n
* @return int
*/
public int climbStairs(int n) {
if (n < 0) {
return 0;
}
return climbStairs0(n);
}
private int climbStairs0(int n) {
if (n == 0 || n == 1) {
return 1;
}
return climbStairs0(n - 1) + climbStairs0(n - 2);
}
}
/* EOF */