/** * @author rale * 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? * Note: Given n will be a positive integer. */ publicclassClimbingStairs {
/** * @author rale * 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? * Note: Given n will be a positive integer. */ publicclassClimbingStairs { publicintclimbStairs(int n){ if(n<=1){ return1; } intcountTwo=1; inttotal=1; while(countTwo*2<=n){ //此处应设为long,防止值溢出 longtemp=1; for(inti= n-countTwo, j = 1 ; j<=countTwo; i--,j++){ temp = temp*i/j; } total += temp; countTwo++; } return total; } }