Skip to content

Commit bed9899

Browse files
committed
0070. Climbing Stairs
1 parent 1b0de32 commit bed9899

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

markdown/0070. Climbing Stairs.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [70\. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
2+
3+
Difficulty: **Easy**
4+
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+
```
15+
Input: 2
16+
Output: 2
17+
Explanation: There are two ways to climb to the top.
18+
1\. 1 step + 1 step
19+
2\. 2 steps
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: 3
26+
Output: 3
27+
Explanation: There are three ways to climb to the top.
28+
1\. 1 step + 1 step + 1 step
29+
2\. 1 step + 2 steps
30+
3\. 2 steps + 1 step
31+
```
32+
33+
34+
#### Solution
35+
36+
Language: **Java**
37+
38+
```java
39+
class Solution {
40+
   public int climbStairs(int n) {
41+
       int[] dp = new int[n + 1];
42+
       return this.climbStairs(dp, n);
43+
44+
  }
45+
46+
   private int climbStairs(int[] dp, int n) {
47+
       if (n <= 1) {
48+
           return 1;
49+
      }
50+
       if (dp[n] > 0) {
51+
           return dp[n];
52+
      }
53+
       int num = climbStairs(dp, n - 1) + climbStairs(dp, n - 2);
54+
       dp[n] = num;
55+
       return num;
56+
  }
57+
}
58+
```
59+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190720171004.png)

src/main/java/leetcode/_70_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._70_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println( solution.climbStairs(0));
10+
System.out.println( solution.climbStairs(2));
11+
System.out.println( solution.climbStairs(3));
12+
System.out.println( solution.climbStairs(1));
13+
System.out.println( solution.climbStairs(111));
14+
}
15+
}
16+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode._70_;
2+
3+
class Solution {
4+
public int climbStairs(int n) {
5+
int[] dp = new int[n + 1];
6+
return this.climbStairs(dp, n);
7+
8+
}
9+
10+
private int climbStairs(int[] dp, int n) {
11+
if (n <= 1) {
12+
return 1;
13+
}
14+
if (dp[n] > 0) {
15+
return dp[n];
16+
}
17+
int num = climbStairs(dp, n - 1) + climbStairs(dp, n - 2);
18+
dp[n] = num;
19+
return num;
20+
}
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [70\. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
2+
3+
Difficulty: **Easy**
4+
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+
```
15+
Input: 2
16+
Output: 2
17+
Explanation: There are two ways to climb to the top.
18+
1\. 1 step + 1 step
19+
2\. 2 steps
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: 3
26+
Output: 3
27+
Explanation: There are three ways to climb to the top.
28+
1\. 1 step + 1 step + 1 step
29+
2\. 1 step + 2 steps
30+
3\. 2 steps + 1 step
31+
```
32+
33+
34+
#### Solution
35+
36+
Language: **Java**
37+
38+
```java
39+
class Solution {
40+
   public int climbStairs(int n) {
41+
       int[] dp = new int[n + 1];
42+
       return this.climbStairs(dp, n);
43+
44+
  }
45+
46+
   private int climbStairs(int[] dp, int n) {
47+
       if (n <= 1) {
48+
           return 1;
49+
      }
50+
       if (dp[n] > 0) {
51+
           return dp[n];
52+
      }
53+
       int num = climbStairs(dp, n - 1) + climbStairs(dp, n - 2);
54+
       dp[n] = num;
55+
       return num;
56+
  }
57+
}
58+
```
59+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190720171004.png)

0 commit comments

Comments
 (0)