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 ...
随机推荐
- 访问树中的所有元素(DOM)
创建一个函数,给定页面上的DOM元素,将访问元素本身及其所有后代(而不仅仅是它的直接子代).对于访问的每个元素,函数应将该元素传递给提供的回调函数. 函数的参数应该是: 一个DOM元素 一个回调函数( ...
- Node.js Error: Cannot find module express的解决办法(转载)
1.全局安装express框架,cmd打开命令行,输入如下命令: npm install -g express express 4.x版本中将命令工具分出来,安装一个命令工具,执行命令: npm in ...
- linux定时跑php脚本,防止重复跑,死循环
$PHP_SELF = realpath($_SERVER['PHP_SELF']); if (!($argc > 1)) { $lock_file = $PHP_SELF . '.lock'; ...
- LR11中web_save_timestamp_param
时间戳是现在时间减去现在的时间 减去 1970年1月1日0点00 的时间 ,然后换算成毫秒. 所以我们需要借助 web_save_timestamp_param 来实现. web_save_times ...
- C# 基础才是重中之重~对象的生与死
为何要写 之所以写这篇文章,完全是因为学生们在实际开发中遇到的问题,一个对象占用的内存空间总不被释放,导致系统内存不断攀升,其最主要原因是我们对“对象的生与死”不清楚,或者从来没有认真去考虑过这件事, ...
- 【JZOJ5338】【NOIP2017提高A组模拟8.25】影子 点分治?/ 排序
题面 65 看到路径问题,就想到了套路:点分治. 对于一个分治中心,先把在其子树的结点的sum和mn求出来,分别表示该节点到分治中心的边权和和点权最小值. 然后把mn离散化,并插入权值线段树中,以su ...
- JAVA--养成好的编程习惯
原文地址:http://user.qzone.qq.com/1159340782/2 略微牺牲一些性能,换来更好的可读性和可维护性是好的选择.以下不提倡和提倡的写法仅供参考. 代码中不提倡的写法 序号 ...
- 独立版的 Asio安装与使用
Asio分为独立版和Boost版.两者使用方法基本一致,只是头文件不同.Boost版是作为Boost的子库提供的. 因为Asio的组织形式为hpp文件(不同一般的C++项目区分头文件.h和源文件.cp ...
- MySQL忘记root密码重置密码(5.7版本)
网上找了一堆方法都不行,经过折腾一番,发现MySQL不同版本重置密码也存在一定的差异!记录下Mysql5.7.18版本的重置密码方法. 1.找到/etc/mysql/my.cnf 修改此文件添加一行s ...
- MyBatis Oracle批量插入
1.oracle如何insert into 多个values https://www.cnblogs.com/mq0036/p/6370224.html?utm_source=itdadao& ...