LeetCode *** 70. Climbing Stairs
2016-04-06 23:56
489 查看
题目:
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?
分析:
爬楼梯问题,一开始用了递归,于是TLE了。。于是用了一个数组来记录之前走的方法数。当前楼梯数n的走法可以由n-1和n-2时的走法一起得到。
代码:
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?
分析:
爬楼梯问题,一开始用了递归,于是TLE了。。于是用了一个数组来记录之前走的方法数。当前楼梯数n的走法可以由n-1和n-2时的走法一起得到。
代码:
class Solution { public: int climbStairs(int n) { int* stairs=new int[n+1]; stairs[1]=1; stairs[2]=2; for(int i=3;i<=n;++i){ stairs[i]=stairs[i-1]+stairs[i-2]; } return stairs ; } };
相关文章推荐
- 【一天一道LeetCode】#11Container With Most Water
- 【一天一道LeetCode】#11Container With Most Water
- 训练集(train set) 验证集(validation set) 测试集(test set)
- Count ways to reach the n’th stair
- Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect
- leetcode——70——Climbing Stairs
- 最简实例说明WAIT、NOTIFY、NOTIFYALL的使用方法
- HDOJ 1789-Doing Homework again【贪心】
- RAII&智能指针
- POJ-1273-Drainage Ditches(最大流)dinic实现 后续模板待补充
- Mastering Bitcoin(掌握比特币)读书笔记
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
- vitualbox + debain 配置NAT+HOSTONY的固定IP
- LeetCode 11 Container With Most Water
- Factorial Trailing Zeroes 带测试版
- LeetCode *** 217. Contains Duplicate
- zoj 1094 Matrix Chain Multiplication
- aircarck-ng
- RAII和模拟实现智能指针
- 微软CEO 纳德拉将赌注押在人工智能上