[LeetCode] 016: Climbing Stairs

来源:互联网 发布:售楼软件有哪些 编辑:程序博客网 时间:2024/06/17 19:28
[Problem]

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?


[Solution]
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= 0)return 0;
if(n <= 2)return n;

// n >= 3
int array[n+1];
for(int i = 1 ; i <= n; ++i){
if(i <= 2)
array[i] = i;
else
array[i] = array[i-1] + array[i-2];
}
return array[n];
}
};
说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击