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. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  2. zombodb sql functions 说明

    zombodb 提供了好多方便的sql 函数工具类以及帮助函数 查看zombodb 版本zdb.version() select * from zdb.version(); version ----- ...

  3. 第0章 概述及常见dos命令

    计算机发展史 计算机的发展历史有多长?真正意义上的计算机诞生,距今也只有80多年的时间.80年,对于每一个人来说,是很长的时间,但对于整个历史来说,只是短短的一瞬间. 从第一代电子计算机的发明,到今天 ...

  4. Mac 永久添加 环境变量方法

    在 ~ 目录下 新建 .bash_profile 文件 在文件新增 export PATH="$PATH:/Users/zhangpengchao/tools/flutter/flutter ...

  5. nginx实现按日期进行日志分割

    1:nginx的访问日志按日期分割,也就是每天的零点把前一天的访问日志以日期的形式备份,然后重新打开一份访问日志,这里的kill -USR1 $pid 重新打开访问日志,必须得把原来的mv,如果存在的 ...

  6. C++ std::async vs async/await in C# - Stack Overflow

    C++ std::async vs async/await in C# - Stack Overflow 我想知道新的c ++功能std::async是否与两个C#关键字async / await相当 ...

  7. 小程序https请求,http网站升到https

    最近开发小程序,因为以前只写过小程序的前端没注意接口,现在才发现原来所有的接口都必须使用https协议了,马上研究了一波,顺便也想给自己的博客升成https的. 申请免费证书 哈哈没办法就是喜欢免费的 ...

  8. Consul之:服务注册与发现

    一.服务的管理(注册与发现)有三种方式: 1:通过配置文件的方式静态注册2:通过HTTP API接口来动态注册(spring cloud使用方式,spring cloud中使用的是consul api ...

  9. Error:Execution failed for task :app:transformClassesWithInstantRunForDebug解决方案

    转自https://blog.csdn.net/student9128/article/details/53026990

  10. Quartz的API简介及Jobs和Trigger介绍

    Quartz的API: 主要api: The key interfaces of the Quartz API are: Scheduler - the main API for interactin ...