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

[week 10][Leetcode][Dynamic Programming] Climbing Stairs

2017-06-26 19:06 435 查看
Question:

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. 

Analysis:
这是一个爬梯子的问题,要到达第n级阶梯则只能是从n-1级阶梯一步跨上来或者是从n-2级阶梯一次跨两步上来,由此很容易可以得到状态转移方程为:
S
= S[n-1] + S[n-2]。代码如下所示:
Code:
class Solution {
public:
int climbStairs(int n) {
int result = 0;
int temp[n+1] = {1};
temp[1] = 1;
for (int i=2;i<=n;i++)
{
temp[i] = temp[i-1] + temp[i-2];
}
return temp
;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: