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. webpack学习笔记(三)

    访问网址: https://github.com/webpack/analyse "scripts": { "dev-build": "webpack ...

  2. kafka-producer partitioner.class的使用

    partitioner.class的说明   在API客户端中封装好的partition( )方法会为消息选择一个分区编号.为了保证消息负载均衡到每个分区,可以通过使用默认方式或者 手动配置这个参数的 ...

  3. golang cache--go-cache

    go-cache是一款类似于memached 的key/value 缓存软件.它比较适用于单机执行的应用程序. go-cache实质上就是拥有过期时间并且线程安全的map,可以被多个goroutine ...

  4. 用a标签实现submit提交按钮的效果

    今天做了一个小项目练手,要求点击a标签后实现post提交的效果,看到这个的时候心理还是有一丝丝懵逼的,不过在朕的十秒钟思考之后有了头绪... 首先表单 <form action="te ...

  5. php语法基础(相比C语言)

    前言 php的语法跟C语言很类似,相信有一定C的基础的人学起来会非常快. 本篇主要介绍php相比C语言有差异的地方 php代码标记 ASP标记:<% 代码 %> 短标记:<? 代码 ...

  6. springMVC框架返回JSON到前端日期格式化

    在Controller类中,需要返回JSON的方法上加上注释@ResponseBody,这是必须的. 然后spring-servlet.xml配置如下: <?xml version=" ...

  7. Bootstrap 前端UI框架

    Bootstrap 有哪些优越性? 1.简单灵活的用于搭建WEB页面的HTML,CSS, JavaScript的工具集 2.基于html5, css3, 具有良好特性,友好的学习曲线,卓越的兼容性,1 ...

  8. vue仿淘宝订单状态的tab切换效果

    <div class="navigation">  //这里是通过循环遍历出来的数据,你需要根据index的值来判断你现在点击的是第几个tab栏导航,同时在js中写一个 ...

  9. java面试题收集

    http://www.cnblogs.com/yhason/archive/2012/06/07/2540743.html 2,java常见面试题 http://www.cnblogs.com/yha ...

  10. 44个Java代码性能优化总结

    https://blog.csdn.net/xiang__liu/article/details/79321639 ---稍后有时间整理