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. 深入浅出node(3) 异步I/O

    这篇主要整理深入浅出Node.js第三章 异步I/O 一) 异步I/O的原因 二)异步I/O实现现状 2.1 异步I/O与非阻塞I/O 2.2 轮询 2.3 理想的非阻塞异步I/O 2.4 现实的异步 ...

  2. javascript 函数初探 (二)--- 那些年的预定义函数

    javascript的预定义函数: javascript引擎中有一组可以随时调用的内建函数. 这些内建函数包括: 1. parseInt() 2. parseFloat() 3. isNaN() 4. ...

  3. iOS 学习 - 16.绘制虚线

    //绘制虚线 -(void)set{ UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )]; [ ...

  4. sublime text 乱码生成.dump问题的解决方法

    title: sublime text 乱码生成.dump问题的解决方法 tags: sublime text,sublime text 3,.dump,乱码 grammar_cjkRuby: tru ...

  5. 自动化运维,远程交互从服务器A上ssh到服务器B上,然后执行服务器B上的命令。

    第一种: ftp -v -n 192.168.0.1 21 <<! user ftp ftp123 bay ! 第二种: { echo -e "\n" echo -e ...

  6. parawork平台介绍

     ParaWork软件项目估算平台---科学估算项目,让管理更简单 ParaWork软件项目估算平台是由北京软件造价评估技术创新联盟与北京科信深度科技有限公司联合开发.维护的软件项目估算工具,为会员免 ...

  7. Symantec Backup Exec Remote Agent 2010在Redhat Enterprise 6.6上启动问题

    在Red Hat Enterprise Linux Server release 6.6 (Santiago)上安装了Symantec Backup Exec Remote Agent 2010后,启 ...

  8. tomcat 设置jvm内存

    修改 tomcat安装目录\bin\catalina.bat在set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%这行下面加上 set JAVA_OPTS=%JAVA_ ...

  9. 不同版本SQL SERVER备份还原时造成索引被禁用

    以下测试例子以SQL 2008备份,在SQL2014还原,造成索引被禁用. --备份环境(SQL Server 2008 R2) /* MicrosoftSQL Server 2008 R2 (RTM ...

  10. Spring中多配置文件以及寻觅引用其他bean的方式

    Spring多配置文件有什么好处? 按照目的.功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理.数据源等少改动的配置与配置bean单独分开. Spring读取配置文件的几种方式: ...