Skip to content

Commit cb64d7e

Browse files
authored
Update Climbing Stairs.java
1 parent 509b60f commit cb64d7e

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Easy/Climbing Stairs.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
class Solution {
2-
public int climbStairs(int n) {
3-
if (n == 1) {
4-
return 1;
2+
public int climbStairs(int n) {
3+
Integer[] dp = new Integer[n + 1];
4+
return climbStairs(dp, n);
55
}
6-
int[] dp = new int[n + 1];
7-
dp[1] = 1;
8-
dp[2] = 2;
9-
for (int i = 3; i <= n; i++) {
10-
dp[i] = dp[i - 1] + dp[i - 2];
6+
7+
private int climbStairs(Integer[] dp, int n) {
8+
if (n == 1) {
9+
return 1;
10+
}
11+
if (n == 2) {
12+
return 2;
13+
}
14+
if (dp[n] != null) {
15+
return dp[n];
16+
}
17+
return dp[n] = climbStairs(dp, n - 1) + climbStairs(dp, n - 2);
1118
}
12-
return dp[n];
13-
}
1419
}

0 commit comments

Comments
 (0)