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 ...
随机推荐
- IntelliJ IDEA 配置 Tomcat 运行web项目
运行前提: 配置好 Java 的运行环境 配置好 Tomcat 安装 IntelliJ IDEA 开始 1.创建项目并配置 关于配置SDK,等建完项目再说 没有配置SDK的话 会出现下面的弹框,点击 ...
- NodeJS基础入门-Buffer
Buffer.byteLength console.log(Buffer.byteLength('test')); console.log(Buffer.byteLength('我是C语言爱好者')) ...
- Robot Framework user guide
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
- 分享几个简单的技巧让你的 vue.js 代码更优雅
1. watch 与 computed 的巧妙结合 一个简单的列表页面. 你可能会这么做: created(){ this.fetchData() }, watch: { keyword(){ thi ...
- python的标准模块
本文用于记录python中的标准模块,随时更新. decimal模块(解决小数循环问题): >>> import decimal >>> a = decimal.D ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- zoj 4049
Halting Problem Time Limit: 1 Second Memory Limit: 65536 KB In computability theory, the haltin ...
- Linux学习-X Server 配置文件解析与设定
X server 的配置 文件都是预设放置在 /etc/X11 目录下,而相关的显示模块或上面提到的总总模块,则主要放置在/usr/lib64/xorg/modules . 比较重要的是字型文件与芯片 ...
- Linux学习-灾难复原的考虑
硬件损毁,且具有完整备份的数据时 由于是硬件损毁,所以我们不需要考虑系统软件的不稳定问题,所以可以直接将完整的系统复原回去 即可. 由于软件的问题产生的被攻破资安事件 由于系统的损毁是因为被攻击,此时 ...
- fhqtreap - Luogu 2464 [SDOI2008]郁闷的小J
[SDOI2008]郁闷的小JJ 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的 ...