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?

原题链接:https://oj.leetcode.com/problems/climbing-stairs/

题目:你在爬楼梯。

须要 n 步才干到顶部。

每次你爬1 或 2 步。

有多少种独立的爬到顶部的方式?

思路:首先非常easy就想到了递归的解法。可是超时了。

	public int climbStairs(int n) {
if(n < 0)
return 0;
if(n <= 1)
return 1;
return climbStairs(n - 1) + climbStairs(n - 2);
}

所以採用非递归的方式。事实上此题类似于求斐波那契数列的和,可是递归不仅慢还可能溢出。以下採用非递归的方法,当中pre代表前n-1台阶的方法数,current代表第n台阶的方法数。

	public int climbStairs(int n) {
if (n == 0 || n == 1)
return 1;
int pre = 1;
int current = 1;
for (int i = 2; i <= n; i++) {
int temp = current + pre;
pre = current;
current = temp;
}
return current;
}

LeetCode——Climbing Stairs的更多相关文章

  1. [LeetCode] 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: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  3. [LeetCode] Climbing Stairs (Sequence DP)

    Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...

  4. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  5. [Leetcode] 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] Climbing Stairs 斐波那契数列

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. leetcode Climbing Stairs python

    class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...

  8. [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  9. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

随机推荐

  1. .Net路(十三)导出数据库到EXCEL

    .NET出口Office文件(word,excel)有两种方法我明白.一个存储在导出的文件中server录以下.利用response输出到浏览器地址栏,直接打开:还有直接利用javascript来导出 ...

  2. PHP移动互联网开发笔记(2)——变量及常量

    原文地址:http://www.php100.com/html/php/rumen/2014/0326/6703.html 一.PHP5.4的基本的语法格式 1.PHP的切割符 view source ...

  3. Android获取SIM卡信息--TelephonyManager

    1>获得TelephonyManager  TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SER ...

  4. hdu4734(数位dp)

    hdu4734 给定 a和b, 问区间[0,b]内有多少个数字的f(i) <=f(a) dp[i][s] 表示i位的数字的f<=s 所以比如如果第i+1位选择数字5之后, 那么只要剩下的i ...

  5. substance的使用示例(转)

    可以使用substance改变界面的皮肤和主题,让Java制作的界面“炫”起来 . 可以下载substance.jar文件 在代码中你可以用: static { try { try { UIManag ...

  6. 安装zookeeper集群

    zookeeper集群的安装   顾名思义zookeeper就是动物园管理员,他是用来管hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase和 Apache So ...

  7. mongodb笔记2

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  8. Swing多线程编程(转)

    关键字: Swing,多线程,GUI,SwingWorker 摘要: 本文论述了怎样开发多线程的Swing程序,从而提高Swing程序的响应速度和性能.     近期,我将推出一系列研究Swing程序 ...

  9. BootStrap -- Grid System

    <script src="jquery.1.9.js"></script> <script src="js/bootstrap.min.js ...

  10. Gmail POP3设定

    好几个同事在问我怎样使用ThunderBird和OE收取IT CHT的邮箱,因为IT CHT就是用Gmail的功能,因此收发邮件是跟Gmail一样,下面是Gmail的POP&SMTP的设置方法 ...