题目

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层阶梯情况下,走到顶端的路径种数(要求每次只能上1层或者2层阶梯)。

这是一个动态规划的题目:

n = 1 时 ways = 1;

n = 2 时 ways = 2;

n = 3 时 ways = 3;



n = k 时 ways = ways[k-1] + ways[k-2];

明显的,这是著名的斐波那契数列问题。有递归和非递归两种方式求解,但是亲测递归方式会出现 The Time Limit异常,所以只能采用非递归计算,可以用一个动态数组保存结果。

AC代码


class Solution {
public:
int climbStairs(int n) { //如果0层,返回0
if (n <= 0)
return 0;
//如果只有1层阶梯,则只有一种方式
else if (n == 1)
return 1;
//若有两层阶梯,则有两种方式(每次走一层,一次走两层)
else if (n == 2)
return 2; int *r = new int[n];
//其余的情况方式总数 = 最终剩余1层的方式 + 最终剩余两层阶梯的方式
r[0] = 1;
r[1] = 2; for (int i = 2; i < n; i++)
r[i] = r[i - 1] + r[i - 2]; int ret = r[n - 1];
delete []r;
return ret;
}
};

GitHub测试程序源码

LeetCode(70) Climbing Stairs的更多相关文章

  1. LeetCode之旅(16)-Climbing Stairs

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

  2. LeetCode(70): 爬楼梯

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

  3. Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)

    Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...

  4. Qt 学习之路 2(70):进程间通信

    Qt 学习之路 2(70):进程间通信 豆子 2013年11月12日 Qt 学习之路 2 16条评论 上一章我们了解了有关进程的基本知识.我们将进程理解为相互独立的正在运行的程序.由于二者是相互独立的 ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. LuoguP1948 电话线 【二分答案/图论】

    其实是和奥格瑞玛一样的题啦. 但还是想了很久后看了题解. 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N&l ...

  2. libcurl在centos6.4 64位操作系统上不支持https的解决方案

    rpm -e –nodeps 软件名 例如: 使用yum install nginx 安装了nginx, 这是如果直接使用yum remove nginx 的话,会把依赖的一些包也删掉, 所以要使用r ...

  3. [POI2007]山峰和山谷Grz

    Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...

  4. 洛谷 P3804 【模板】后缀自动机

    来一份模板 #include<cstdio> #include<algorithm> #include<cstring> #include<queue> ...

  5. L - Prime Number(求n内质数的个数)

    Description Write a program which reads an integer n and prints the number of prime numbers which ar ...

  6. Codeforces Round #235 (Div. 2) D (dp)

    以为是组合,后来看着像数位dp,又不知道怎么让它不重复用..然后就没思路 了. 其实状压就可以了 状压是可以确定一个数的使用顺序的 利用01也可以确定所有的数的使用以及不重复 dp[i+1<&l ...

  7. AJPFX总结泛型概念和使用

    泛型泛型(generic)概述和基本使用                泛型把明确数据类型的操作放到创建对象或者调用方法的时候再明确.                                J ...

  8. apache设置无缓存

    打开httpd.conf 开启扩展 确保开启 LoadModule headers_module modules/mod_headers.so 添加配置项 并添加以下配置,跟据文件类型来让浏览器每次都 ...

  9. 黑马程序员----java基础:异常

    dff ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 经常写程序的人对try...catch...finally语句肯定是不陌生的了.但是好多 ...

  10. ASP.NET Eval四种绑定方式 及详解

    1.1.x中的数据绑定语法 <asp:Literal id="litEval2" runat="server" Text='<%#DataBinde ...