70. 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?
Note: Given n will be a positive integer.
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
分析
这道题是斐波那契数列的模型,上第n阶台阶的方法数是f(n)=f(n-1)+f(n-2),因为到第n阶台阶只有从第n-1阶台阶上来或者从第n-2阶上来;
class Solution {
public:
int climbStairs(int n) {
int fab[n+1];
fab[0]=fab[1]=1;
for(int i=2;i<=n;i++){
fab[i]=fab[i-1]+fab[i-2];
}
return fab[n];
}
};
70. Climbing Stairs(动态规划)的更多相关文章
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- java.io.IOException: The same input jar is specified twice
简介: eclipse android proguard 打包时出现 java.io.IOException: The same input jar is specified twice 错误, 这里 ...
- bzoj1008 [HNOI2008]越狱——快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...
- Spark SQL中 RDD 转换到 DataFrame
1.people.txtsoyo8, 35小周, 30小华, 19soyo,882./** * Created by soyo on 17-10-10. * 利用反射机制推断RDD模式 */impor ...
- idea ssm项目移包报错问题
写完代码之后发现包结构太乱了 想要规划一下 结果报错 这里面的包路径都可以点进去,还是报找不到com.lf.company.entity.Business 后来发现是 在移动前和移动后都存在这个m ...
- mongoDB的基本用法
一.MongoDB初识 什么是MongoDB MongoDB是一个基于分布式文件存储的数据库.由c++语言编写.旨在为web应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库 ...
- mybatis 中 foreach 的性能问题及调优
1.mybatis中最初的sql语句 SELECT 参数1, 参数2, 参数3 FROM 表 WHERE 条件参数1 in <foreach item="item" inde ...
- cocos2d-js 添加广告
http://www.cocoachina.com/bbs/read.php?tid=225655
- MVC系列学习(十四)-路由规则及路由调试工具
1.本次学习的代码,比较简单,就是在路由配置文件中,添加一个路由信息:同时添加一个相应的控制器及视图 控制器中代码如下 即有两条路由匹配规则,一个Kim控制器,该控制器下有个Index的方法,和一个对 ...
- python gdal 修改shp文件的属性值
driver = ogr.GetDriverByName('ESRI Shapefile')datasource = driver.Open(shpFileName, 1)layer = dataso ...
- Android RecyclerView notifyDataSetChanged不起作用
一般listview设置完data后调用notifyDataSetChanged便可刷新布局界面,然而recycleview调用这个方法却没有任何反应.对于很多不熟悉recycleview的话很容易躺 ...