[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:
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 ; } };
相关文章推荐
- leetcode week 10
- Leetcode:215. Kth Largest Element in an Array (week 10)
- Leetcode:23. Merge k Sorted Lists (week 10)
- LeetCode 题解(Week 10): 467. Unique Substrings in Wraparound String
- leetCode->Climbing Stairs
- [LeetCode]Climbing Stairs
- leetcode hard模式专杀之10Regular Expression Matching
- [Leetcode] Climbing Stairs
- [Leetcode 10, Hard] Regular Expression Matching
- leetcode解题方案--070--Climbing Stairs
- Climbing stairs--LeetCode
- leetcode -- Climbing Stairs
- (java)leetcode-70:Climbing Stairs
- Leetcode:240. Search a 2D Matrix II (week 11)
- [LeetCode]Climbing Stairs(Java)
- LeetCode - Climbing Stairs
- 【Machine Learning】【Andrew Ng】- Quiz(Week 10)
- leetcode题解c++ | 10. Regular Expression Matching
- LeetCode-Climbing Stairs(爬楼梯问题)
- LeetCode(70) Climbing Stairs