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 ...
随机推荐
- g++编译多个源原文件和头文件(转载)
(头文件名是:Sales_item.hpp 源文件名:7-31.cpp和Sales_item.cpp)(点击我下载源文件) 方法1: 我用的命令是:g++ -o 7-31 7-31.cpp Sales ...
- poj 1679 The Unique MST 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- spring cloud深入学习(二)-----服务注册中心spring cloud eureka
服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...
- 大量的Close_wait 发现的 too many open file 错
突然频繁出现大量的 Close_wait,查看程序日志,发现 connection 在 accept 时报错,Too many open file. 估计程序里有这个漏洞,当 accept 时报错,没 ...
- C# WPF 仿QQ靠近屏幕上方自动缩起功能实现
碰到了类似需求,但是上网查了一圈发现感觉要么很复杂,要么代码很臃肿,索性自己实现了一个 几乎和QQ是一模一样的效果,而且核心代码只有20行左右. 代码如下: using System; using S ...
- Django 创建web项目之HelloWorld
Django.Flask.Tornado并称为python WEB三大框架.Diango是一个开源的web应用框架,具有开发速度快的特点.同时因为过度封装,具有性能低的特点. 创建Django项目,启 ...
- 爬取简书图片(使用BeautifulSoup)
import requests from bs4 import BeautifulSoup url_list = [] kv = {'User-Agent':'Mozilla/5.0'} r = re ...
- ubuntu设置终端命令历史记录
----------------------------------------------- HISTTIMEFORMAT='%F %T ' # 使用HISTTIMEFORMAT在历史中显示TIME ...
- mybatis添加数据返回主键
程序结构图: 表结构: 创表sql: Create Table CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `us ...
- linux 查看磁盘空间占用情况
工作中有时被分配的测试机空间不大,经常遇到磁盘空间占满的情况.排查过程如下: 一.首先使用df -h 命令查看磁盘剩余空间,通过以下图看出/目录下的磁盘空间已经被占满. 二.进入根目录,因为最近常用的 ...