70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
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.
https://mp.weixin.qq.com/s/0AgJmQNYAKzVOyigXiKQhA, 动态规划入门最好材料,也是该题个人认为最完美的解释,没有之一! 非常感谢该文章的作者和发给我文章的张春玲老师!
自个代码:
\(O(n)\) time, \(O(1)\) extra space.
// A textbook style question for Dynamic Programming.
// I like it!
int climbStairs(int n) {
// boundarys
if (n == 1) return 1;
if (n == 2) return 2;
// state transfer formula
int a = 1, b = 2, temp;
for (int i = 3; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return temp;
}
70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)的更多相关文章
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
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 Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 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 ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
- 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< ...
- 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 ...
随机推荐
- ELK学习总结(2-2)单模式CRUD操作
------------------------------------------------------ 1.查看索引信息 请求命令: GET /library/_settings GET /li ...
- ASP.NET Web API编程——序列化与内容协商
1 多媒体格式化器 多媒体类型又叫MIME类型,指示了数据的格式.在HTTP协议中多媒体类型描述了消息体的格式.一个多媒体类型包括两个字符串:类型和子类型. 例如: text/html.image/p ...
- Mac 中配置Apache
使用的mac版本是10.10.1,mac自带的Apache环境 分为两部分: 1.启动Apache 2.设置虚拟主机 启动Apache 打开终端, >>sudo apachectl -v, ...
- python 杂货铺
python 杂货铺之不知道的python操作 1.交互模式下的神奇的_ windos中cmd交互模式中下(python2,python3),最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个 ...
- SpringMVC(七):@RequestMapping下使用POJO对象绑定请求参数值
Spring MVC会按照请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值,支持级联属性. 如:address.city.dept.address.province等. 步骤一:定义Ac ...
- Ajax实现注册无刷新验证用户名是否存在
1. [代码][JavaScript]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
- laravel整合JWT遇到的问题及解决方案
1.在执行 php artisan jwt:secret 生成密钥的时候,报错Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() do ...
- [LeetCode] Pour Water 倒水
We are given an elevation map, heights[i] representing the height of the terrain at that index. The ...
- 【温故而知新】HTTP 概述
什么是 HTTP 官方解释是 "因特网的多媒体信使",通俗点说,就是个送信的.电话机出来之前,人与人(有一定距离)之间的沟通基本靠写信,然后由快递员送发.如果把 web 服务器和客 ...
- ●BZOJ 4453 cys就是要拿英魂!
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4453 题解: 后缀数组,离线询问,栈看了一堆题解才看懂,太弱啦 ~ 如果对于一个区间[l,r ...