Skip to content

Commit

Permalink
LeetCode70.爬楼梯
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxuechen committed Jun 29, 2018
1 parent e60e39d commit 088cbd4
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions LeetCode1/LeetCode70.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package leetCode;

/**
* 假设你正在爬楼梯。需要 n 步你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
* @author yangxuechen
*
*/
public class LeetCode70 {

private static int[] dp;
public static void main(String[] args) {
dp=new int[40];
for(int i=0;i<dp.length;i++) {
dp[i]=0;
}
int n=fun2(40);
System.out.println(n);
}
//递归的方法解决
public static int fun1(int n) {
if(n==1||n==2) {
return n;
}
return fun1(n-1)+fun1(n-2);
}
//动态规划思想解决
public static int fun2(int n) {
if(n==1||n==2) {
return n;
}
if(dp[n-1]==0) {
dp[n-1]=fun2(n-1);
}
if(dp[n-2]==0) {
dp[n-2]=fun2(n-2);
}
return dp[n-1]+dp[n-2];

}
}

0 comments on commit 088cbd4

Please sign in to comment.