File tree Expand file tree Collapse file tree 4 files changed +155
-0
lines changed
src/main/java/leetcode/_70_ Expand file tree Collapse file tree 4 files changed +155
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments