LeetCode70 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? (Easy)
分析:
经典的爬楼梯问题,对于第n阶可以从n - 1阶跳一步上来,也可以从n - 2阶跳两步上来,设dp[n]为到第n阶的方案数,
则dp[n] = dp[n - 1] + dp[n - 2]。
不过实现的时候采用自底向上的方式,不要用递归(大量重复计算)。
也算是一道动态规划的入门题吧。
代码:
class Solution {
public:
int climbStairs(int n) {
if (n == ) {
return ;
}
if (n == ) {
return ;
}
int a = , b = , c = a + b;
for (int i = ; i <= n; ++i) {
c = a + b;
a = b;
b = c;
}
return c;
}
};
LeetCode70 Climbing Stairs的更多相关文章
- leetcode70—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)
70. 爬楼梯 70. Climbing Stairs 题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意: 给定 ...
- LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)
题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
随机推荐
- mac mamp host 配置
<VirtualHost *:80> DocumentRoot "/Users/xuxu/www" ServerName localhost <Directory ...
- 使用yarn代替npm
npm node module package,是nodeJs的包管理工具,最初是有 Isaac Z. Schlueter 开发的,这个让全世界的人都可以很快的运用互相开发的package的工具使no ...
- Binder对象死亡通知机制
本文參考<Android系统源码情景分析>,作者罗升阳. 一.Binder库(libbinder)代码: ~/Android/frameworks/base/libs/bin ...
- Robbin负载均衡
Robbin是在Spring Cloud中的一个组件,是由Netfix发布的负载均衡器,有助于控制HTTP和TCP客户端的行为.它给我们提供了默认的轮询.随机等负载均衡算法.同时也可以由我们定义自己的 ...
- 洛谷P1029 最大公约数和最小公倍数问题 [2017年6月计划 数论02]
P1029 最大公约数和最小公倍数问题 题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1 ...
- Vue-- vue-preview(图片查看器)的使用步骤:
vue-preview的使用步骤: )下载 )配置:找到配置文件加入: { test: /vue-preview.src.*?js$/, loader: 'babel-loader' }, 在处理ur ...
- web服务发展历程
PhP发展历史1.php: 开始名字含义:personal home page 个人网页 现在名字含义:HyperText Perprocessor 超文本预处理语言 预处理: 说明PHP是在服务器预 ...
- Ubuntu 12.04 安装 IQQ
1. 安装 IQQ 首先应安装jdk包 2. 百度网盘下载: http://pan.baidu.com/share/home?uk=3071047022 3. 运行 (1) Linux用户给IQQ-1 ...
- windows中将网络共享文件夹映射为网络硬盘
目的是: 实现局域网,不同电脑之间共享文件. 例如: 计划将A电脑 的文件夹C:\MM ,共享给局域网电脑 B. 局域网所有电脑都可访问: 1. 在A电脑中 共享文件夹..选择‘启用网络发现’ ‘ ...
- Django框架Day1------之url和views
一.新建一个Django程序(window 7进入cmd里面操作):注意,此处要需在指定的文件夹下 a,django-admin startproject django_test(django_tes ...