forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.java
39 lines (27 loc) · 819 Bytes
/
Solution1.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
/// Source : https://leetcode.com/problems/climbing-stairs/description/
/// Author : liuyubobobo
/// Time : 2017-11-18
import java.util.Arrays;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution1 {
private int[] memo;
public int climbStairs(int n) {
if(n <= 0)
throw new IllegalArgumentException("n must be greater than zero");
memo = new int[n+1];
Arrays.fill(memo, -1);
return calcWays(n);
}
private int calcWays(int n){
if(n == 0 || n == 1)
return 1;
if(memo[n] == -1)
memo[n] = calcWays(n - 1) + calcWays(n - 2);
return memo[n];
}
public static void main(String[] args) {
System.out.println((new Solution1()).climbStairs(10));
}
}