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?

题意:爬台阶问题。每次能爬一个或两个台阶,问一个有n个台阶的话,一共有几种方法爬到顶端。

思路:

  n<=1,此时只有一种。

  n>1时,对于每一个台阶i,要到达台阶,最后一步都有两种方法,从i-1迈一步,或从i-2迈两步。

  也就是说到达台阶i的方法数=达台阶i-1的方法数+达台阶i-2的方法数。所以该问题是个DP问题。

d(0) = 1

d(1) = 1

d(2) = d(2-1) + d(2-2)

d(3) = d(3-1) + d(3-2)

……

好吧,状态转移方程其实就是Fibonacci数列。

代码实现给出两种方案吧:

python代码如下:

 class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n<=1:
return 1
res = []
res.append(1)
res.append(1)
for i in range(2,n+1):
res.append(res[-1]+res[-2])
return res[-1]

Java代码如下:

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

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. Java [Leetcode 70]Climbing Stairs

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

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

  10. 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. 图标字体(IconFont)制作

    图标字体(IconFont)介绍 图标字体(IconFont)现在越来越被广泛使用,大大提高了网页的多样化,解决了视网膜屏幕失真的问题. 据说微软从IE4开始支持的这个私有方法(@font-face) ...

  2. Linux安全基础:vi的使用

    1.vi的三种模式(1)一般模式(2)编辑模式(3)指令模式 2.模式切换键入i/o/a进入编辑模式键入:/,或/进入指令模式按esc退回一般模式保存wq强制保存wq!退出q强制退出q! 3.一般模式 ...

  3. 自动显示隐藏布局的listView

    借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示 ...

  4. OC 类别与扩展(匿名类别)

    OC 类别与扩展(匿名类别) 类别(Categroy): 又称为扩展类,在类的原基础上扩展方法,且不可添加变量,如果扩展的方法与原始类中的方法相同,则会隐藏原始方法,且不可在扩展方法中通过super调 ...

  5. Android 热修复技术(1)---原理

    热修复技术分为几部分: 原理介绍 Android HotFix源码分析 自定义框架 1.Android分包MultiDex原理 首先Dex是什么东西? Dex就是Window里面的exe文件 也就是可 ...

  6. Linux No volume control GStreamer plugins and/or devices found

    案例环境:Oracle Linux Server release 5.7 进入Oracle Linux系统后,在右上角点击声音图标时,则会弹出如下报错窗口: The volume control di ...

  7. mysql5.7.1 zip版本安装记录

    很久没遇到使用mysql的项目了,今天下载了个,发现变了很多,搜索了下,发现按http://www.cnblogs.com/wenthink/p/MySQLInstall.html中提到的基本可以使用 ...

  8. C++ 栈和堆的区别

    C++中的存储区分为全局数据区.代码区.堆.栈. 全局数据区存放静态数据.全局变量.常量. 代码区存放所有类成员函数和非成员函数的代码. 栈区存放用于函数的返回地址.形参.局部变量.返回类型. 堆区存 ...

  9. springmvc学习资料整理

    springmvc接口开发返回XML/JSON数据 1.SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上):http: ...

  10. 锐捷与Vmare网络冲突解决办法

    首先,禁用掉VMare的两个适配器,然后在我的电脑右键管理中找到服务,把VMare的所有服务开起来,并把手动改成自动,最后把VMare的网络设置设置为NAT连接即可.