Skip to content

Commit 92f91c1

Browse files
refactor 70
1 parent 706384a commit 92f91c1

File tree

1 file changed

+8
-13
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+8
-13
lines changed

src/main/java/com/fishercoder/solutions/_70.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@
3232

3333
public class _70 {
3434
public static class Solution1 {
35-
//classical dp problem
3635
public int climbStairs(int n) {
37-
if (n < 1) {
38-
return 0;
39-
} else if (n < 4) {
36+
if (n == 1) {
4037
return n;
41-
} else {
42-
int[] dp = new int[n + 1];
43-
//the number of ways to reach step n could be calculated from n-1 and n-2
44-
dp[1] = 1;
45-
dp[2] = 2;
46-
for (int i = 3; i <= n; i++) {
47-
dp[i] = dp[i - 1] + dp[i - 2];
48-
}
49-
return dp[n];
5038
}
39+
int[] dp = new int[n + 1];
40+
dp[1] = 1;
41+
dp[2] = 2;
42+
for (int i = 3; i <= n; i++) {
43+
dp[i] = dp[i - 1] + dp[i - 2];
44+
}
45+
return dp[n];
5146
}
5247
}
5348
}

0 commit comments

Comments
 (0)