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 No70.ClimbingStairs; public class Solution { public static int climbStairs(int n) { if(n<=0) return 0; if(n-3>0){ return climbStairs(n-2)+climbStairs(n-1); } else{ if(n==3) return 3; else { if (n==2){ return 2; } else{return 1;} } } } public static int climbStairs1(int n) { if (n==0) return 0; int k=0;//1的数量 int climbNum=0; boolean[] isHas=new boolean[n+1]; while(k<=n){ int i=(n-k)%2; int j=(n-k)/2; if(isHas[k+i]==false){ climbNum+=c_cal(j+i+k,i+k); } isHas[k+i]=true; k++; } return climbNum; } public static int c_cal(int a,int b){ if(b==0) return 1; int c1=1; int c2=1; for(int i=0;i<b;i++){ c1=c1*(a-i); c2=c2*(b-i); } System.out.print(a+":"+b+" "); return c1/c2; } public static int climbStairs3(int n) { if(n <= 3){ return n; } else{ return climbStairs3(n/2)*climbStairs3(n-n/2)+climbStairs3(n/2-1)*climbStairs3(n-n/2-1); } } public static void main(String[] args){ System.out.print(""+climbStairs3(50)); } }
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No70.ClimbingStairs
原文地址:http://blog.csdn.net/buptgshengod/article/details/43888725