题目描述:

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?

解题思路:

利用DP的方法,一个台阶的方法次数为1次,两个台阶的方法次数为2个。n个台阶的方法可以理解成上n-2个台阶,然后2步直接上最后一步;或者上n-1个台阶,再单独上一步。

代码如下:

public class Solution {
public int climbStairs(int n) {
int p2 = 1, p1 = 2, current = 0;
if(n <= 2)
return n;
for(int i = 2; i < n; i++){
current = p2 + p1;
p2 = p1;
p1 = current;
}
return current;
}
}

  

Java [Leetcode 70]Climbing Stairs的更多相关文章

  1. 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 ...

  2. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  7. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Redis 安装与配置

    启动 Redis WINDOW 服务: https://github.com/ServiceStack/ServiceStack.Redis install-package ServiceStack. ...

  2. Filter及FilterChain的使用详解(转)

    一.Filter的介绍及使用 什么是过滤器? 与Servlet相似,过滤器是一些web应用程序组件,可以绑定到一个web应用程序中.但是与其他web应用程序组件不同的是,过滤器是"链&quo ...

  3. 01-07-01【Nhibernate (版本3.3.1.4000) 出入江湖】并发控制

    Nhibernate 并发控制 [1]悲观并发控制 正在使用数据的操作,加上锁,使用完后解锁释放资源. 使用场景:数据竞争激烈,锁的成本低于回滚事务的成本 缺点:阻塞,可能死锁 [2]乐观并发控制: ...

  4. WinForm 资源文件的使用

    1. 创建资源文件: 2.双击资源文件,打开如下图:添加一个字符串: 名称为cnnstr 值为-- 3.添加文本文件和图像 4. 调用代码 MessageBox.Show(Resource1.cnns ...

  5. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  6. java split IP地址要用双斜杠

    示例代码: public void test() { String address = "11.12.13.14:800"; System.out.println(address. ...

  7. 修改linux文件/文件夹权限

    事情缘起:在VirtualBox虚拟机Ubuntu 12.04里通过共享文件夹从物理机拷贝jdk,拷贝过来之后不能正常使用.用javac -version命令不能查看java版本信息,sudo可以.原 ...

  8. go学习资料及优秀博文

    相关书籍: Go Web编程 http://blog.csdn.net/broadview2006/article/details/8875097 优秀博文: 系统编程语言明日之星—Go(http:/ ...

  9. C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) 1 /* 2 * 计算该日在本年中是第几天,注意闰年问题 3 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 4 * 特殊情况,闰年且 ...

  10. js捕捉浏览器关闭事件-兼容几乎所有浏览器

    很多时候我们都在困扰,如何捕获浏览器关闭事件,网上虽然有很多方法,但都不理想,后来终于找到了一个很好地实现方法,大家可以试试哦,支持几乎所有的浏览器 <script type="tex ...