-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path070.java
More file actions
67 lines (66 loc) · 1.58 KB
/
070.java
File metadata and controls
67 lines (66 loc) · 1.58 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//fibonacci,time complexity O(n),space complexity O(1)
class Solution {
public int climbStairs(int n) {
if(n<=0){
return 0;
}
if(n==1){
return 1;
}
if(n==2){
return 2;
}
int oneStepBefore=2;
int twoStepBefore=1;
int allways=0;
for(int i=3;i<=n;i++){
allways=oneStepBefore+twoStepBefore;
twoStepBefore=oneStepBefore;
oneStepBefore=allways;
}
return allways;
}
}
//dp ,time complexity O(n),space complexity O(n)
//注意这里尽量别区考虑n=0的情况,如果把dp[0]=0会得到错误的结果
class Solution {
public int climbStairs(int n) {
if(n<=0){
return 0;
}
if(n==1){
return 1;
}
int[] dp=new int[n+1];//如果只申请n个长度的数组,dp[0]实际上指的是台阶1,这里方面理解申请n+1长度的数组
dp[1]=1;
dp[2]=2;
for(int i=3;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}
}
//recursion+memory ,time complexity O(n),space complexity O(n)
class Solution {
public int climbStairs(int n) {
if(n<=0){
return 0;
}
int[] tem=new int[n];
return climbStairs(0, n, tem);
}
public int climbStairs(int step, int n, int[] tem) {
if (step > n) {
return 0;
}
if (step == n) {
return 1;
}
if (tem[step] > 0) {
return tem[step];
}
tem[step] = climbStairs(step + 1, n, tem) + climbStairs(step + 2, n, tem);
//System.out.println("step="+step);
return tem[step];
}
}