Skip to content

Commit b63d7b7

Browse files
refactor 70
1 parent ef6ffbd commit b63d7b7

File tree

1 file changed

+38
-14
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-14
lines changed
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
package com.fishercoder.solutions;
22

3-
/**Leetcode 70: You are climbing a stair case. It takes n steps to reach to the top.
3+
/**
4+
* 70. Climbing Stairs
5+
6+
You are climbing a stair case. It takes n steps to reach to the top.
7+
8+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
9+
10+
Note: Given n will be a positive integer.
11+
12+
Example 1:
13+
14+
Input: 2
15+
Output: 2
16+
Explanation: There are two ways to climb to the top.
17+
18+
1. 1 step + 1 step
19+
2. 2 steps
20+
21+
Example 2:
22+
23+
Input: 3
24+
Output: 3
25+
Explanation: There are three ways to climb to the top.
26+
27+
1. 1 step + 1 step + 1 step
28+
2. 1 step + 2 steps
29+
3. 2 steps + 1 step
30+
31+
*/
432

5-
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?*/
633
public class _70 {
34+
public static class Solution1 {
735
//classical dp problem
836
public int climbStairs(int n) {
9-
if (n < 1) {
10-
return 0;
11-
}
12-
if (n < 4) {
13-
return n;
14-
}
37+
if (n < 1) {
38+
return 0;
39+
} else if (n < 4) {
40+
return n;
41+
} else {
1542
int[] dp = new int[n + 1];
1643
//the number of ways to reach step n could be calculated from n-1 and n-2
1744
dp[1] = 1;
1845
dp[2] = 2;
1946
for (int i = 3; i <= n; i++) {
20-
dp[i] = dp[i - 1] + dp[i - 2];
47+
dp[i] = dp[i - 1] + dp[i - 2];
2148
}
2249
return dp[n];
50+
}
2351
}
24-
25-
public static void main(String... strings) {
26-
_70 test = new _70();
27-
System.out.println(test.climbStairs(6));
28-
}
52+
}
2953
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy