【LeetCode從零單排】No70.ClimbingStairs
題目
爬樓梯問題,這是一道很有趣的問題。首先看題目:
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?
1.遞迴(對應程式碼climbStairs)
如果樓梯有n層,我們回退到n-2和n-1步的時候,發現climbStairs(n)=climbStairs(n-1)+climbStairs(n-2)。
為什麼?因為當還剩兩步的時候,有兩種情況,走一個2步,或兩個1,但是其中一種情況和climbStairs(n-1)有重複,所以得到如上式子。
這種方法的效率較差,leetcode不接受
2.折中演算法(對應程式碼climbStairs3)
通過上面的講解,其實只要是任意拆分n為n1,n2和n1+1,n2-1兩項就可以。climbStairs(n)=climbStairs(n1)*climbStairs(n2)+climbStairs(n1-1)*climbStairs(n2+1)
這種演算法效率最優
3.直接算(對應程式碼climbStairs1)
k代表1的個數,k從0~n。算出每種情況情況的1步和2步的個數,然後通過牛頓公式演算法。但是階乘較大的時候int型會溢位
程式碼
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從零單排】No38.CountAndSayLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- 【LeetCode從零單排】No96 Unique Binary Search TreesLeetCode
- 【LeetCode從零單排】No112 Path SumLeetCode
- 【LeetCode從零單排】No.169 Majority Element(hashmap用法)LeetCodeHashMap
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 從零單排學Redis【白銀】Redis
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode
- 從零單排學Redis【黃金】Redis
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode
- 【LeetCode從零單排】No118 Pascal's TriangleLeetCode
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode
- 【LeetCode從零單排】No 114 Flatten Binary Tree to Linked ListLeetCode
- 【LeetCode從零單排】No 191.Number of 1 Bits(考察位運算)LeetCode
- 從零單排學Redis【鉑金二】Redis
- 從零單排學Redis【鉑金一】Redis
- 【LeetCode從零單排】No102 Binary Tree Level Order TraversalLeetCode
- 【LeetCode從零單排】No.135Candy(雙向動態規劃)LeetCode動態規劃
- 「從零單排canal 07」 parser模組原始碼解析原始碼