您的位置:首页 > 大数据 > 人工智能

[LeetCode]Climbing Stairs

2015-08-23 00:49 399 查看
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?

基础的DP,到达n的方法等于到达n-1的方法加上n-2的方法

class Solution {
public:
int climbStairs(int n) {
int n1=1,n2=1;
int all;
int i = 2;
if(n<2) return 1;
while(i<=n){
all = n1 + n2;
n2 = n1;
n1 = all;
++i;
}
return all;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: