-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode70.java
More file actions
32 lines (29 loc) · 790 Bytes
/
LeetCode70.java
File metadata and controls
32 lines (29 loc) · 790 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
import java.util.HashMap;
public class LeetCode70 {
public static void main(String[] args) {
// 输入:n = 2
// 输出:2
System.out.println(new Solution70().climbStairs(2));
// 输入:n = 3
// 输出:3
System.out.println(new Solution70().climbStairs(3));
}
}
class Solution70 {
private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
if (map.containsKey(n)) {
return map.get(n);
} else {
int result = climbStairs(n - 1) + climbStairs(n - 2);
map.put(n, result);
return result;
}
}
}