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. UC高级编程--实现myls程序

    跟着达内视频,学习UC高级编程,完毕程序小练习. 主要练习的函数为:  int lstat(const char *path, struct stat *buf);  size_t strftime( ...

  2. SWT入门-常用组件的使用(转)

    转自:http://www.cnblogs.com/kentyshang/archive/2007/08/16/858367.html swt的常用组件button ,text ,combo,list ...

  3. Android 一些错误

    android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...

  4. WordPress更改固定链接出现404的解决方案

    很多站长在玩WordPress的时候,可能会碰到一个问题,就是想把WordPress伪静态,在后台设置好固定链接之后,就会出现文章页面或者所有的页面都出现404错误.解决方法如下: 1,.htacce ...

  5. windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息)

    原文:windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息) 前几天做项目用到, 代码贴给大家. /// <summary> /// 获取当前位置的经纬度 ...

  6. Windows Phone开发(21):做一个简单的绘图板

    原文:Windows Phone开发(21):做一个简单的绘图板 其实我们今天要说的就是一个控件--InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实 ...

  7. USACO comehome Dijkstra

    USER: Kevin Samuel [kevin_s1] TASK: comehome LANG: C++ Compiling... Compile: OK Executing... Test 1: ...

  8. 华为-on练习--小写字符数的统计显示

    主题: 手动输入一个字符串,只有小写字母,统计每个字符和输出频率中出现的串,输出.提示可以使用map 样例:输入:aaabbbccc 输出:a 3 b 3 c 3 分析: 看到后面的提示,简直就是不用 ...

  9. 苹果公司的新的编程语言 Swift 高级语言(十一)--初始化类的析构函数的一个实例

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程. 初始化包含设置一个实例的每个存储属性为一个初始值,以及运行不论什么其他新的实例可以使用之前须要的设置或 ...

  10. 朴素UNIX它-Linux CFS注视

    该系列产品,被称为纯UNIX,但它也包含各种类别UNIX该系统的细节,自从完成我多年的学习笔记本系列文章,分析了各种UNIX,类UNIX思想和情感的实现. 这篇文章是比较短.只是分析Linux CFS ...