C++實(shí)現(xiàn)LeetCode(70.爬樓梯問題)
[LeetCode] 70. Climbing Stairs 爬樓梯問題
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.
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
這篇博客最開始名字叫做爬梯子問題,總是有童鞋向博主反映移動(dòng)端打不開這篇博客,博主覺得非常奇怪,自己也試了一下,果然打不開。心想著是不是這個(gè)博客本身有問題,于是想再開一個(gè)相同的帖子,結(jié)果還是打不開,真是見了鬼了。于是博主換了個(gè)名字,結(jié)果居然打開了?!進(jìn)經(jīng)過排查后發(fā)現(xiàn),原來是“爬梯子”這三個(gè)字是敏感詞,放到標(biāo)題里面,博客就被屏蔽了,我也真是醉了,完全是躺槍好么,無奈之下,只好改名為爬樓梯問題了 -。-|||。
這個(gè)爬梯子問題最開始看的時(shí)候沒搞懂是讓干啥的,后來看了別人的分析后,才知道實(shí)際上跟斐波那契數(shù)列非常相似,假設(shè)梯子有n層,那么如何爬到第n層呢,因?yàn)槊看沃荒芘?或2步,那么爬到第n層的方法要么是從第 n-1 層一步上來的,要不就是從 n-2 層2步上來的,所以遞推公式非常容易的就得出了:dp[n] = dp[n-1] + dp[n-2]。 由于斐波那契額數(shù)列的求解可以用遞歸,所以博主最先嘗試了遞歸,拿到 OJ 上運(yùn)行,顯示 Time Limit Exceeded,就是說運(yùn)行時(shí)間超了,因?yàn)檫f歸計(jì)算了很多分支,效率很低,這里需要用動(dòng)態(tài)規(guī)劃 (Dynamic Programming) 來提高效率,代碼如下:
C++ 解法一:
class Solution { public: int climbStairs(int n) { if (n <= 1) return 1; vector<int> dp(n); dp[0] = 1; dp[1] = 2; for (int i = 2; i < n; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp.back(); } };
Java 解法一:
public class Solution { public int climbStairs(int n) { if (n <= 1) return 1; int[] dp = new int[n]; dp[0] = 1; dp[1] = 2; for (int i = 2; i < n; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n - 1]; } }
我們可以對(duì)空間進(jìn)行進(jìn)一步優(yōu)化,只用兩個(gè)整型變量a和b來存儲(chǔ)過程值,首先將 a+b 的值賦給b,然后a賦值為原來的b,所以應(yīng)該賦值為 b-a 即可。這樣就模擬了上面累加的過程,而不用存儲(chǔ)所有的值,參見代碼如下:
C++ 解法二:
class Solution { public: int climbStairs(int n) { int a = 1, b = 1; while (n--) { b += a; a = b - a; } return a; } };
Java 解法二:
public class Solution { public int climbStairs(int n) { int a = 1, b = 1; while (n-- > 0) { b += a; a = b - a; } return a; } }
C++ 解法三:
class Solution { public: int climbStairs(int n) { vector<int> memo(n + 1); return helper(n, memo); } int helper(int n, vector<int>& memo) { if (n <= 1) return 1; if (memo[n] > 0) return memo[n]; return memo[n] = helper(n - 1, memo) + helper(n - 2, memo); } };
Java 解法三:
public class Solution { public int climbStairs(int n) { int[] memo = new int[n + 1]; return helper(n, memo); } public int helper(int n, int[] memo) { if (n <= 1) return 1; if (memo[n] > 0) return memo[n]; return memo[n] = helper(n - 1, memo) + helper(n - 2, memo); } }
論壇上還有一種分治法 Divide and Conquer 的解法,用的是遞歸形式,可以通過,但是博主沒有十分理解,希望各位看官大神可以跟博主講一講~
C++ 解法四:
public class Solution { public int climbStairs(int n) { if(n <= 1) return 1; return climbStairs(n / 2) * climbStairs(n - n / 2) + climbStairs(n / 2 - 1) * climbStairs(n - n / 2 - 1); } }
Java 解法四:
public class Solution { public int climbStairs(int n) { if(n <= 1) return 1; return climbStairs(n / 2) * climbStairs(n - n / 2) + climbStairs(n / 2 - 1) * climbStairs(n - n / 2 - 1); } }
其實(shí)斐波那契數(shù)列是可以求出通項(xiàng)公式的,推理的過程請(qǐng)參見 知乎上的這個(gè)貼子,那么有了通項(xiàng)公式后,直接在常數(shù)級(jí)的時(shí)間復(fù)雜度范圍內(nèi)就可以求出結(jié)果了,參見代碼如下:
C++ 解法五:
class Solution { public: int climbStairs(int n) { double root5 = sqrt(5); return (1 / root5) * (pow((1 + root5) / 2, n + 1) - pow((1 - root5) / 2, n + 1)); } };
Java 解法五:
public class Solution { public int climbStairs(int n) { double root5 = Math.sqrt(5); double res = (1 / root5) * (Math.pow((1 + root5) / 2, n + 1) - Math.pow((1 - root5) / 2, n + 1)); return (int)res; } }
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(70.爬樓梯問題)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)爬樓梯問題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)要解讀C++的動(dòng)態(tài)和靜態(tài)關(guān)聯(lián)以及虛析構(gòu)函數(shù)
這篇文章主要介紹了簡(jiǎn)要解讀C++的動(dòng)態(tài)和靜態(tài)關(guān)聯(lián)以及虛析構(gòu)函數(shù),析構(gòu)函數(shù)在C++編程中平時(shí)并不是太常用,需要的朋友可以參考下2015-09-09C語(yǔ)言?超詳細(xì)模擬實(shí)現(xiàn)單鏈表的基本操作建議收藏
單鏈表是后面要學(xué)的雙鏈表以及循環(huán)鏈表的基礎(chǔ),要想繼續(xù)深入了解數(shù)據(jù)結(jié)構(gòu)以及C語(yǔ)言,我們就要奠定好這塊基石!接下來就和我一起學(xué)習(xí)吧2022-03-03c++ qsort 與sort 對(duì)結(jié)構(gòu)體排序?qū)嵗a
這篇文章主要介紹了c++ qsort 與sort 對(duì)結(jié)構(gòu)體排序?qū)嵗a,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下2020-11-11C++ 實(shí)現(xiàn)求最大公約數(shù)和最小公倍數(shù)
這篇文章主要介紹了c++ 實(shí)現(xiàn)求最大公約數(shù)和最小公倍數(shù)的相關(guān)資料,需要的朋友可以參考下2017-05-05詳解C語(yǔ)言編程中的函數(shù)指針以及函數(shù)回調(diào)
這篇文章主要介紹了C語(yǔ)言編程中的函數(shù)指針以及函數(shù)回調(diào),函數(shù)回調(diào)實(shí)際上就是讓函數(shù)指針作函數(shù)參數(shù)、調(diào)用時(shí)傳入函數(shù)地址,需要的朋友可以參考下2016-04-04C/C++實(shí)現(xiàn)遍歷文件夾最全方法總結(jié)
這篇文章主要為大家介紹了C/C++實(shí)現(xiàn)遍歷文件夾功能的最全方法總結(jié),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11