LeetCode Everyday -- 70
来源:互联网 发布:摄像安卓软件 编辑:程序博客网 时间:2024/05/21 08:35
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?
class Solution {public: int climbStairs(int n) { int a,b,c; a = 1; b = 2; if(n == 1){ return a; } if(n == 2){ return b; } for(int i =2;i < n;i++){ c = a + b; a = b; b = c; } return c; }};
解题思路:题目是一个动态规划的问题,因为每次只能走1或2步,所以到达最后一步之前有两种选择。设d(n)是到达第n级阶梯的解,则d(n) = d(n-1) + 1 + d(n-2) + 2,所以可以通过递归推出d(n),跟斐波那契数列很像,同样,如果n比较大,递归效率会很低,所以采用动态规划方法,将d(n-1)和d(n-2)记录下来,推出d(n).
0 0
- LeetCode Everyday -- 70
- LeetCode Everyday --226
- LeetCode Everyday --171
- LeetCode Everyday -- 253
- LeetCode Everyday--52
- LeetCode Everyday -- 108
- LeetCode Everyday -- 83
- LeetCode Everyday -- 141
- LeetCode Everyday: 1. Two Sum
- Leetcode Everyday: 344. Reverse String
- Leetcode Everyday: 292. Nim Game
- Leetcode Everyday: 266. Palindrome Permutation
- Leetcode Everyday: 293. Flip Game
- Leetcode Everyday: 283. Move Zeroes
- Leetcode Everyday: 100. Same Tree
- Leetcode Everyday: 242. Valid Anagram
- Leetcode Everyday: 252. Meeting Rooms
- Leetcode Everyday: 217. Contains Duplicate
- HDU - 5198 - Strange Class
- tsung 分布式压测工具
- 选课小系统
- 利用UIView做出霓虹灯的效果
- WIN7下面Codeblocks+wxWidgets环境搭建
- LeetCode Everyday -- 70
- 235 Lowest Common Ancestor of a Binary Search Tree
- HDU 4979 A simple math problem.
- nginx相关博客
- 编译器实践四 之 FIRST集合,NULLABLE集合,FOLLOW集合
- HALCON算子函数——Chapter 15 : Segmentation
- hdoj畅通工程【并查集】
- 收集关于iOS编程的优秀博客(不定时更新)
- ZOJ 3447 Doraemon's Number Game(Java优先队列·BigInteger)