[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 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
-----------------------------------------------------------
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
题意:
有n阶台阶,可以每次爬1步或者2步, 爬到终点有多少种不同的方法
思路:
一维dp
用一个数组存下当前 k (k<=n)阶台阶有多少种不同的走法
发现规律
爬上n阶台阶 = 爬上n-1阶台阶再爬1阶 + 爬上n-2阶台阶再爬2阶
爬上n-1阶台阶 = 爬上n-2阶台阶再爬1阶 + 爬上n-3阶台阶再爬2阶
爬上n-2阶台阶 = 爬上n-3阶台阶再爬1阶 + 爬上n-4阶台阶再爬2阶
初始化,
dp[0] = 1
dp[1] = 1
转移方程,
dp[i] = dp[i-1] + dp[i-2]
代码:
class Solution {
public int climbStairs(int n) {
int [] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= n; i++){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
}
[leetcode]70. Climbing Stairs爬楼梯的更多相关文章
- [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 70. Climbing Stairs爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 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 ...
- 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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- PythonStudy——函数的返回值 The return value of the function
# 在函数体中,通过return关键词返回函数的内部数据给外部 """# 一.作用# return作用:1.结束函数:2.将函数的内部数据返回给外部 def fn(): ...
- xen虚拟化平台虚拟机在线新加一块磁盘无法识别
截图 报错:you need to shutdown and then restart the vm before it can access the new disk 添加好磁盘后按照提示重启虚拟机 ...
- django基础 -- 8.cookie 和 session
一. cookie 1.cookie 的原理 工作原理是:浏览器访问服务端,带着一个空的cookie,然后由服务器产生内容, 浏览器收到相应后保存在本地:当浏览器再次访问时,浏览器会自动带上Cooki ...
- Day 08 文件操作模式,文件复制,游标
with open:将文件的释放交给with管理 with open('文件', '模式', encoding='utf-8') as f: # 操作 pass a模式:追加写入 # t ...
- 十七、springcloud(三)服务的注册与调用
1.启动服务注册中心Eureka(见上篇) 启动成功后,暂时无服务 2.项目框架 3.创建服务提供者(spring-cloud-houge-provider)jar a.application.pro ...
- bootstrapValidator关于js,jquery动态赋值不触发验证(不能捕获“程序赋值事件”)解决办法
//触发oninput事件 //propertychange 兼容ie678 $('#captainName').on('input propertychange', function() { }); ...
- 右击菜单一键优化(增加新建office2003、新建reg和bat,删除新建公文包、新建wps、新建rar)
右击菜单一键优化(增加新建office2003.新建reg和bat,删除新建公文包.新建wps.新建rar) Windows Registry Editor Version 5.00 [HKEY_CL ...
- 如何将maven依赖项打进jar包,将一个完整的项目打进jar包
目的:我的目的就是将项目所有的文件,包括pom文件中依赖的jar包都打进一个jar包里面. 过程:将下面的内容放到pom文件里面. </dependencies> <build> ...
- git 在本地拉取远程分支的代码(并不做提交操作)
1. git fetch 获取远程的所有分支 2. 在执行 git checkout -b local-branch-name origin/remote-branch 就可以将远程分支remote ...
- IDEA VM设置
1.IDEA vm options -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128 ...