你正在爬楼梯。需要 n 步你才能到达顶部。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方式可以爬到楼顶呢?
注意:给定 n 将是一个正整数。
示例 1:
输入: 2
输出: 2
说明: 有两种方法可以爬到顶端。
1.  1 步 + 1 步
2.  2 步
示例 2:
输入: 3
输出: 3
说明: 有三种方法可以爬到顶端。
1.  1 步 + 1 步 + 1 步
2.  1 步 + 2 步
3.  2 步 + 1 步
详见:https://leetcode.com/problems/climbing-stairs/description/

Java实现:

方法一:

class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2){
return n;
}
return climbStairs(n-1)+climbStairs(n-2);
}
}

方法二:

class Solution {
public int climbStairs(int n) {
if(n<2){
return n;
}
int tmp=0,pre=1,res=1;
for(int i=2;i<=n;++i){
tmp=res;
res=pre+res;
pre=tmp;
}
return res;
}
}

方法三:

class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2){
return n;
}
int[] dp=new int[n];
dp[0]=1;
dp[1]=2;
for(int i=2;i<n;++i){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n-1];
}
}

070 Climbing Stairs的更多相关文章

  1. Java for LeetCode 070 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】070. Climbing Stairs

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  3. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  6. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  7. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  8. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  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. BZOJ_3672_ [Noi2014]购票_CDQ分治+斜率优化

    BZOJ_3672_ [Noi2014]购票_CDQ分治+斜率优化 Description  今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参 ...

  2. 【LeetCode】039. Combination Sum

    题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...

  3. AtCoder Grand Contest 028 A:Two Abbreviations

    题目传送门:https://agc028.contest.atcoder.jp/tasks/agc028_a 题目翻译 给你两个串\(s\)与\(t\),长度分别为\(n,m\).问你存不存在一个串长 ...

  4. rmmod: chdir(/lib/modules): No such file or directory

    内核版本:linux3.4.20 交叉编译器:arm-linux-gcc 4.3.3 busybox :  busybox 1.20 问题: 使用rmmod会出现 rmmod : chdir(/lib ...

  5. XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译

    XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译 <?xml version="1.0" encoding="UTF-8" ...

  6. Runtime.getRuntime()

    1转自:https://www.aliyun.com/jiaocheng/849282.html 那就首先说点Runtime类吧,他是一个与JVM运行时环境有关的类,这个类是Singleton的.我说 ...

  7. chromium浏览器开发系列第五篇:Debugging with WinDBG

    Windbg 相信windows开发的人都知道,有些人用的溜儿溜儿的,有个crash,直接拿这个工具一分析,就定位出来了.非常好用.以前有个同事,做sdk开发 的,会各种命令.来北京后,还去过微软面试 ...

  8. php学习笔记-PHP中的几个取整函数

    floor是向下取整,比如4.5,它是在4和5之间的一个数,那么结果就是4. ceil是向上取整,比如3.7,它是在3和4之间的一个数,那么结果就是4. round是对一个数四舍五入,小数部分如果小于 ...

  9. Python 在windows上安装BeautifulSoup和request以及小案例

    Python以及PyCharm安装成功后,操作如下: 此时,代码import requests不报错了. 那么,Python 在windows上安装BeautifulSoup,怎么操作呢? 1. 打开 ...

  10. js中“||”和“&&”的高级用法

    例1:用于赋值&&:从左往右依次判断,当当前值为true则继续,为false则返回此值(是返回未转换为布尔值时的原值哦)|| : 从左往右依次判断,当当前值为false则继续,为tru ...