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爬楼梯的更多相关文章

  1. [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 ...

  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 ...

  3. 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 ...

  4. Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)

    题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...

  5. 70. Climbing Stairs爬楼梯

    网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...

  6. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  7. 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 ...

  8. 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 ...

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. 支持Oracle的模糊查询和精准查询

    相信所有的软件开发者都做过页面上的查询功能,而且很多都需要既支持模糊查询的,也需要支持精准查询的,而且不需要增加多余的功能,只需要在文本框中输入包含类似*之类的符号即可. 下面的方法就是通过*来判断到 ...

  2. [随笔][Golang][golang nil 相关]

    nil 是不能比较的 不同类型的nil的address是一样的 不同类型的nil是不能比较的 nil 是map, slice, pointer, channel, func, interface的零值 ...

  3. Python的深copy和浅copy

    浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象. 浅copy: a = [1, 2, ...

  4. Python的paramiko,实现ssh

    最简单的使用paramiko登录远程机器执行一些命令,学习实验楼的paramiko记录下来,第一次使用ConfigParser这个库,对于封装这些还是不太熟悉,只能慢慢来,嘿嘿嘿 这是python脚本 ...

  5. Java_EE面试题

    Java_EE面试题 欢迎到我的Git仓库去提交您觉得优秀的内容! 1.什么是Servlet? Servlet是用来处理客户端请求并产生动态网页内容的Java类.Servlet主要是用来处理或者是存储 ...

  6. Spark流处理调优步骤

    总体运行状况: 这里的每个批处理任务间隔是10s一次,所以Total Delay是14s,那么对于下一个批处理任务来说就是延迟了14 - 10 = 4s . Total Delay   -  每个批处 ...

  7. docker 进程监控 Dumb-Init进程信号处理 --转自https://blog.csdn.net/tiger435/article/details/54971929

    随着docker及Kubernetes技术发展的越来越成熟稳定,越来越多的公司开始将docker用于生产环境的部署,相比起物理机上直接部署,多了一层docker容器的环境,这就带来一个问题:进程信号接 ...

  8. c++11 function_typetraits备忘

    function traits. 获取函数或成员函数的返回类型,参数类型,参数长度,类类型. 函数参数列表推断基于typelist:http://www.cnblogs.com/flytrace/p/ ...

  9. <亲测>centos7通过yum安装JDK1.8(实际上是openjdk)

    centos7通过yum安装JDK1.8   安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...

  10. Python的hasattr(),getattr(),setattr()

    今天读到源码时遇到了setattr()和getattr()两方法,给忘了,重新回顾一下吧! 1. hasattr(object, name) 判断object里是否有name属性,有就返回True,没 ...