Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
leetcode
/
algorithms
/
java
/
src
/
dynamicProgramming
/
climbStairs
/
climbStairs.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
35 lines (32 loc) · 1016 Bytes
master
Breadcrumbs
leetcode
/
algorithms
/
java
/
src
/
dynamicProgramming
/
climbStairs
/
climbStairs.java
Copy path
Top
File metadata and controls
Code
Blame
35 lines (32 loc) · 1016 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Source : https://oj.leetcode.com/problems/climbing-stairs/
// Inspired by : http://www.jiuzhang.com/solutions/climbing-stairs/
// Author : Lei Cao
// Date : 2015-10-12
/**********************************************************************************
*
* You are climbing a stair case. It takes n steps to reach to the top.
*
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*
*
**********************************************************************************/
package dynamicProgramming.climbStairs;
public class climbStairs {
/**
* @param n: An integer
* @return: An integer
*/
public int climbStairs(int n) {
int[] matrix = new int[n];
if (n < 3) {
return n;
}
matrix[0] = 1;
matrix[1] = 2;
// write your code here
for (int i = 2; i < matrix.length; i++) {
matrix[i] = matrix[i-1] + matrix[i-2];
}
return matrix[n-1];
}
}
You can’t perform that action at this time.