你正在爬楼梯。需要 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. (转)RTMP协议从入门到放弃

    转载自:  http://blog.csdn.net/shangmingyang/article/details/50837852 RTMP协议是Real Time Message Protocol( ...

  2. CentOS虚拟机通过主机WIFI上网

    0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过本机的WIFI上外网: (2)虚拟机为VMWare12下的CentOS7系统. 1 虚拟机设置 选择NAT 模式: 2 宿主机W ...

  3. Bind 远程连接DNS服务器时出现 rndc: connection to remote host closed

    使用命令:rndc -s 192.168.1.2 status 连接远程的bind 搭建的DNS服务器时出现下面的错误:   rndc: connection to remote host close ...

  4. iis部署错误:HTTP 错误 500.21 - Internal Server Error

    将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integr”在其模块列表中有一个错 ...

  5. CentOS快速搭建LAMP环境

    LAMP --  Linux Apache MySQL PHP 在CentOS安装的顺序,我一般是Apache -> MySQL -> PHP 第一步.安装并配置Apache 1.使用yu ...

  6. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

  7. Sharepoint2013搜索学习笔记之设置业务数据内容源(六)

    Sharepoint搜索爬网组件支持爬Business Data Connectivity Service 承载的外部数据,关于Business Data Connectivity Service设置 ...

  8. 原生js实现一个侧滑删除取消组件(item slide)

    组件,本质上是解决某个问题封装的类,在此记录原生js实现侧滑删除 先上效果图 实现思路 1. 确定渲染的数据结构 2. 思考划分布局,总的有两个主要的模块:内容区域和按钮区域 2.1 内容区域保持宽度 ...

  9. Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  10. POJ2549【hash分离链接法】

    题意: 给n个不同的数,求一个4个数(a,b,c,d)的组合满足a+b+c=d;求最大的d. 思路: 没想到可以用hash搞/ 这个就是数据结构里的分离链接法~ 解决hash冲突的方法:将所有关键字为 ...