[LeetCode] Climbing Stairs, Solution
来源:互联网 发布:内部优惠券提取软件 编辑:程序博客网 时间:2024/06/14 15:31
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?
» Solve this problem
[Thoughts]
DP. The transition function should be:
f(n) = f(n-1) + f(n-2) n>2;
or = 1 n=1
or = 2 n=2
[Code]
1: int climbStairs(int n) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int fn_2 =1, fn_1=2;
5: if(n==1) return fn_2;
6: if(n ==2) return fn_1;
7: int fn;
8: for(int i =3; i<=n; i++)
9: {
10: fn = fn_2+fn_1;
11: fn_2 = fn_1;
12: fn_1= fn;
13: }
14: return fn;
15: }
0 0
- [LeetCode] Climbing Stairs, Solution
- 70. Climbing Stairs QuestionEditorial Solution
- LeetCode: Climbing Stairs
- LeetCode: Climbing Stairs
- [LeetCode]Climbing Stairs
- LeetCode Climbing Stairs
- [Leetcode] Climbing Stairs
- Leetcode: Climbing stairs
- LeetCode Climbing Stairs
- [LeetCode] Climbing Stairs
- leetcode 107: Climbing Stairs
- [LeetCode] Climbing Stairs
- [LeetCode]Climbing Stairs
- [leetcode]Climbing Stairs
- LeetCode-Climbing Stairs
- [leetcode] Climbing Stairs
- LeetCode - Climbing Stairs
- LeetCode:Climbing Stairs
- Eclipse浏览项目的问题
- [LeetCode] 3 Sum, Solution
- 使用UIImageView的画线方法
- [LeetCode] 3Sum Closest, Solution
- [LeetCode] Add Two Numbers, Solution
- [LeetCode] Climbing Stairs, Solution
- [LeetCode] Combination Sum, Solution
- [LeetCode] Combination Sum II, Solution
- [LeetCode] Combinations, Solution
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal, Solution
- [LeetCode] Container With Most Water, Solution
- [LeetCode] Convert Sorted List to Binary Search Tree, Solution
- [LeetCode] Count and Say, Solution
- [LeetCode] Decode Ways, Solution