LeetCode OJ——Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/
走台阶的题目,转换出数学模型之后,就是Fibonacci数列。后一个数等于前两个数的和。
递归版本超时,代码如下:
class Solution {
public:
    int walk(int sum)
    {
        if(sum ==  )
            return ;
        if(sum ==)
            return ;
        return walk(sum-)+walk(sum-);
    }
    int climbStairs(int n) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        return walk(n);
    }
};
然后改成顺序的了,AC,代码如下:
#include <iostream>
using namespace std;
class Solution {
public:
    int climbStairs(int n) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int* arr = (int*)malloc(sizeof(int)*(n+));
        arr[] = ;
        arr[] = ;
        for(int i = ;i<=n;i++)
            arr[i] = arr[i-]+arr[i-];
        return arr[n];
    }
};
int main()
{
    Solution myS;
    int ans = myS.climbStairs();
    cout<<ans<<endl;
    return ;
}
加油!
LeetCode OJ——Climbing Stairs的更多相关文章
- [LeetCode OJ]-Climbing Stairs
		You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ... 
- [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 ... 
- [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 ... 
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
		Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ... 
- 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 ... 
- Leetcode#70. Climbing Stairs(爬楼梯)
		题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ... 
- 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 ... 
- [leetcode] 14. Climbing Stairs
		这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It tak ... 
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
		翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ... 
随机推荐
- 【线程池】ExecutorService与quartz搭配出现的问题
			问题描述: 使用quartz定时推送微信公众号模板消息,一分钟推送一次,定时器里面使用了一个ExecutorService线程池,大小为5个. 批量获取数据之后,全部数据都被分配到n/5的线程池里面等 ... 
- 《linux设备驱动开发详解》笔记——7并发控制
			linux中并发无处不在,底层驱动需要考虑. 7.1 并发与竞争 7.1.1 概念 并发:Concurrency,多个执行单元同时.并行执行 竞争:Race Condistions,并发的执行单元对共 ... 
- 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装
			参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ... 
- python的标准模块
			本文用于记录python中的标准模块,随时更新. decimal模块(解决小数循环问题): >>> import decimal >>> a = decimal.D ... 
- 【转】MySQL innodb_autoinc_lock_mode 详解 ,并发插入时主键冲突的解决方案
			本文转载于 http://www.cnblogs.com/JiangLe/p/6362770.html innodb_autoinc_lock_mode这个参数控制着在向有auto_increment ... 
- Linux学习-逻辑滚动条管理员 (Logical Volume Manager)
			LVM 可以整合多个实体 partition 在一起, 让这些 partitions 看起来就像是一个磁盘一样!而且,还可以在未来新增或移除其他的实 体 partition 到这个 LVM 管理的磁盘 ... 
- LA 5010 Go Deeper 2-SAT 二分
			题意: 有\(n\)个布尔变量\(x_i\),有一个递归函数.如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层. 问最多能递归多少层. 分析 ... 
- 装饰器与lambda
			装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ... 
- myeclipse中hibernate生成映射文件
			在hibernate中,每个数据表对应的其实是一个实体类,每个实体类有一个对应的hbm.xml配置文件匹配,myeclipse中有个MyEclipse Database Explorer视图,它提供了 ... 
- poj2823 Sliding Window luogu1886 滑动窗口 单调队列
			模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ... 
