Last active
December 29, 2018 02:56
-
-
Save qiaoxu123/3a7ce114fa543485aeaac65c7a7f703d to your computer and use it in GitHub Desktop.
对问题进行分析,发现解决方案的次数满足斐波那契数列的规律,于是采用该方法实现。
- 可以分别使用递归方式和数组方式实现
> 对LeetCode的OJ有点失望了,第一次效率4ms,第二次直接变为0ms。。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使用Fibonacci sequence方法实现 | |
//效率总体来说还是挺高的 | |
//Runtime: 0 ms, faster than 100.00% | |
class Solution { | |
public: | |
int climbStairs(int n) { | |
int Fib[n] = {0}; | |
Fib[0] = Fib[1] = 1; | |
// if(n <= 2) | |
// return n; | |
// int* step = new int[n]; | |
for(int i = 2;i <= n;++i) | |
Fib[i] = Fib[i-1] + Fib[i-2]; | |
return Fib[n]; | |
} | |
// int Fib(int n){ | |
// if(n <= 1) | |
// return 1; | |
// else | |
// return Fib(n - 1) + Fib(n - 2); | |
// } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment