Skip to content

Commit ee812ba

Browse files
authored
Update Climbing Stairs.java
1 parent ebc167f commit ee812ba

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Easy/Climbing Stairs.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
class Solution {
2-
int[] memo;
32
public int climbStairs(int n) {
43
if (n == 1) {
54
return 1;
65
}
7-
memo = new int[n + 1];
8-
memo[1] = 1;
9-
memo[2] = 2;
10-
return helper(n);
11-
}
12-
13-
private int helper(int n) {
14-
if (memo[n] != 0) {
15-
return memo[n];
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];
1611
}
17-
memo[n] = helper(n - 1) + helper(n - 2);
18-
return memo[n];
12+
return dp[n];
1913
}
2014
}

0 commit comments

Comments
 (0)