[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——Python 中Switch-Case 结构的实现
学习Python过程中,发现Python没有Switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch-Case功能. 方法一 ...
- python设置路径值时为什么要输入r
r:代表处理不转义现象 Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果 ...
- Spring Boot - 基础 POM 文件
表 1. Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-b ...
- 黄聪:visual studio 2017编译运行出现脚本发生错误等问题如何解决?
升级VS2017后,编译运行程序会出现 /Community/Common7/IDE/PrivateAssemblies/plugin.vs.js 错误 先说VS2017-15.6.1跟旧版本IE的兼 ...
- Android批量验证渠道、版本号(Linux版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号(Linux版本)使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2../VerifyChannelVe ...
- 安装ORACLE高可用RAC集群11g执行root脚本的输出信息
安装ORACLE高可用RAC集群11g执行root脚本的输出信息 作者:Eric 微信:loveoracle11g [root@node1 ~]# /u01/app/oraInventory/orai ...
- ssh免密钥之上厕所
ssh服务简单介绍 SSH协议框架中最主要的部分是三个协议: *传输层协议(The Transport Layer Protocol)提供服务器认证,数据机密性,信息完整性等的支持; *用户认证协议( ...
- log4j.properties详解
首先建立项目 package a; import org.apache.log4j.Logger; public class Test { private static Logger logger=L ...
- Handling Touches - RN3
1. basic button format: <tag event caption /> <Button onPress={{}} title="I am button& ...
- k8s学习笔记之三:k8s快速入门
一.前言 kubectl是apiserver的客户端工具,工作在命令行下,能够连接apiserver上实现各种增删改查等各种操作 kubectl官方使用文档:https://kubernetes.io ...