Climbing Stairs Solutions in C++
Number 70
Difficulty Easy
Acceptance 47.8%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/climbing-stairs/// Author : Hao Chen// Date : 2014-06-27class Solution {public:int climbStairs(int n) {if (n<=3) return n;int a[2]={2,3};for(int i=4; i<=n; i++){int t = a[0] + a[1];a[0] = a[1];a[1] = t;}return a[1];}//Time too longint climbStairs2(int n) {if (n<=3) return n;return climbStairs(n-1) + climbStairs(n-2);}};
C++ solution by pezy/LeetCode
class Solution {public:int climbStairs(int n) {if (n<0) return 0;else if (n<3) return n;int res = 0;for (int i = 0, a = 1, b = 2; i != n-2; ++i){res = a + b;a = b;b = res;}return res;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-18#include <iostream>#include <vector>#include <cassert>using namespace std;/// Memory Search/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {private:vector<int> memo;int calcWays(int n){if(n == 0 || n == 1)return 1;if(memo[n] == -1)memo[n] = calcWays(n - 1) + calcWays(n - 2);return memo[n];}public:int climbStairs(int n) {assert(n > 0);memo = vector<int>(n + 1, -1);return calcWays(n);}};int main() {cout << Solution().climbStairs(10) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-18#include <iostream>#include <vector>#include <cassert>using namespace std;/// Dynamic Programming/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:int climbStairs(int n) {assert(n > 0);vector<int> memo(n + 1, -1);memo[0] = 1;memo[1] = 1;for(int i = 2 ; i <= n ; i ++)memo[i] = memo[i - 1] + memo[i - 2];return memo[n];}};int main() {cout << Solution().climbStairs(10) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <vector>#include <cassert>using namespace std;/// Fibonacci Number/// We can see from the transition equation that/// climbStairs(i) is the (i+1)th fibonacci number/// where f0 = 0, f(1) = 1, f(2) = 1, f(3) = 2...////// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:int climbStairs(int n) {assert(n > 0);if(n == 1)return 1;int prev = 1, cur = 1;for(int i = 3 ; i <= n + 1; i ++){int f = cur + prev;prev = cur;cur = f;}return cur;}};int main() {cout << Solution().climbStairs(10) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <vector>#include <cassert>using namespace std;/// Fibonacci Number - Binets Method/// | F(n+1) F(n) | = | 1 1 |^n/// | F(n) F(n-1) | | 1 0 |////// Time Complexity: O(logn)/// Space Complexity: O(1)class Solution {public:int climbStairs(int n) {assert(n > 0);if(n == 1)return 1;vector<vector<int>> base = {{1, 1}, {1, 0}};return matrix_pow(base, n)[0][0];}private:vector<vector<int>> matrix_pow(const vector<vector<int>>& m, int n){if(n == 1)return m;vector<vector<int>> t = matrix_pow(m, n / 2);vector<vector<int>> res = matrix_multiply(t, t);if(n % 2)return matrix_multiply(res, m);return res;}vector<vector<int>> matrix_multiply(const vector<vector<int>>& m1,const vector<vector<int>>& m2){vector<vector<int>> res(2, vector<int>(2, 0));res[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0];res[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1];res[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0];res[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1];return res;}};int main() {cout << Solution().climbStairs(10) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <cmath>#include <cassert>using namespace std;/// Fibonacci Number - Closed Formula/// Fn = 1/sqrt(5) * {[(1+sqrt(5))/2]^n - [(1-sqrt(5))/2]^n}////// Time Complexity: O(logn)/// Space Complexity: O(1)class Solution {public:int climbStairs(int n) {assert(n > 0);if(n == 1)return 1;double sqrt5 = sqrt(5.0);return (int)((pow((1 + sqrt5) / 2, n + 1) - pow((1 - sqrt5) / 2, n + 1)) / sqrt5);}};int main() {cout << Solution().climbStairs(10) << endl;return 0;}