你正在爬楼梯。需要 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. Redis高可用部署及监控

    Redis高可用部署及监控 目录                        一.Redis Sentinel简介 二.硬件需求 三.拓扑结构 .单M-S结构 .双M-S结构 .优劣对比 四.配置部 ...

  2. linux命令学习:echo详解,格式化输出,不换行输出

    shell脚本不换行刷新数据 #!/bin/bash ] do a=$(ifconfig eth0 | grep 'RX pac' | awk '{print $2}' | awk -F: '{pri ...

  3. javaScript运算符之in运算符

  4. eclipse 通过maven 开发storm项目

    window系统安装java 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/ind ...

  5. 【Lintcode】069.Binary Tree Level Order Traversal

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  6. 洛谷【P4883】mzf的考验

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  7. 微信小程序 加载 HTML 标签

    肯定有小伙伴遇到过这个问题:加载的数据是一堆HTML 标签这就尴尬了,因为小程序没有提供 webview 来加载这些 HTML.但是不用慌,小程序不提供我们可以自己造个新轮子,自己造不出新轮子咱们找到 ...

  8. LINUX socket网络编程

    1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系统为进程通信提供了相应设施,如 U ...

  9. Android应用如何反馈Crash报告

    转自:http://www.cnblogs.com/draem0507/archive/2013/05/25/3099461.html 一.为什么要Crash crash可以理解成堕落,垮台.按照我们 ...

  10. java基础知识 学习 关于URL中传递的参数含有特殊字符

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码                                   ...