爬楼梯 · Climbing Stairs
[抄题]:
假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?
[思维问题]:
- 不知道一步、两步怎么加。还是用iteration迭代。此题公式可被称为斐波那契数列。
- 不知道和坐标型有什么关系:列j = 1即可
[一句话思路]:
生兔子问题:把大数赋给小数
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 把大数赋给小数
- state是声明有变量,initialize是给变量赋值
[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
746. Min Cost Climbing Stairs最值爬楼梯
[代码风格] :
public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int climbStairs(int n) {
//corner case
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
//state=//initialization
int one_step = 2;
int two_step = 1;
int all_step = 0; //function
for (int i = 2; i < n; i++) {
all_step = one_step + two_step;
two_step = one_step;
one_step = all_step;
}
//result
return all_step;
}
}
爬楼梯 · Climbing Stairs的更多相关文章
- LeetCode 70. 爬楼梯(Climbing Stairs)
70. 爬楼梯 70. Climbing Stairs 题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意: 给定 ...
- [Swift]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(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- [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 ...
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- 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] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
随机推荐
- tomcat 启动超级慢
今天在新环境里部署tomcat, 刚开始启动很快,关闭之后再启动,却发现启动日志打印到 00:25:14.144 [localhost-startStop-1] INFO o.s.web.conte ...
- phpexcel导入数据出现PHPExcel_RichText Object解决办法
在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...
- 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
作者:Tony Peng 去年,OpenAI 的 1v1 AI 击败了世界顶尖选手 Dendi,OpenAI CTO Greg Brockman 承诺:明年,我们会带着 5v5 的 AI bot 重回 ...
- AVL树Python实现
# coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...
- Python之部分基础知识点汇总
1.三元运算(又称三目运算) 三元运算(又称三目运算),简单条件语句的简写 if a<b: A else: B等价于:A if a<b else B 2.
- 怎么爆加密过后的前端JS
网站地址,保密,下面进入主题 页面元素和JS代码 无法看 用谷歌浏览器,选择器选中提交按钮,注意看界面元素源码 onclick="$('#submitForm').submit()" ...
- Dark theme for Texstudio - TeX - LaTeX
Dark theme for Texstudio ~~~ 1.window系统如下操作 ~~~ 1. texstudio的配置文件texstudio 的配置文件在~/.config/texstudi ...
- eclipse xml 编码问题 “3 字节的 UTF-8 序列的字节 3 无效”
原本项目没问题,git commit之后,突然报错 “3 字节的 UTF-8 序列的字节 3 无效” 尝试过改xml文件编码等,没成功.pom中设置属性,成功解决 <project.build. ...
- php缓存类
<?php /* * 缓存类 cache * 实 例: include( "cache.php" ); $cache = new cache(30); $cache-> ...
- WDA-参考路径
1. SAP Netweave安装 https://wenku.baidu.com/view/b3ac371a227916888486d77c.html?sxts=1545717961793 2. ...