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 ...
随机推荐
- numpy中常用的函数
1. power(x1, x2) 对x1中的每个元素求n次方.不会改变x1上午shape. 2. sum(a, axis=None, dtype=None, out=None, keepdims=Fa ...
- C++输入密码不显示明文
之前有遇到需求说输入密码不显示明文,但同时会有一些其他问题,暂时没做,如今经过尝试可以实现,但是得先知道要输入的是密码.主要利用的getch()函数的不回显特点.需要注意的是这个函数不是标准函数,而且 ...
- vue-cli3.0相关的坑
vue-cli3.0相对比vue-cli2.0少了 vue-build.js vue-config.js vue-cli2.0 运行命令 npm run devvue-cli3.0 运行命令 npm ...
- python数据类型之元组(tuple)
元组是python的基础类型之一,是有序的. 元组是不可变的,一旦创建便不能再修改,所以叫只读列表. name = ('alex', 'jack') name[0] = 'mark' # TypeEr ...
- JAVA基础篇—多态
class ColaEmployee父类 package com.cola; public class ColaEmployee { private String name; private int ...
- ACM Changchun 2015 L . House Building
Have you ever played the video game Minecraft? This game has been one of the world's most popular ga ...
- Android后台的linux一直保持唤醒状态,不进入睡眠
由于要做Android手机的电池续航测试,是不能插usb的,所以把case放到sh文件中,之后push到手机里,执行的. 但是出现个问题,假如case中有很长时间的sleep操作,关闭手机屏幕,这样l ...
- BZOJ 2217: [Poi2011]Lollipop
若sum可行 sum-2一定可行 序列和为ans 找出和ans奇偶性不同的最大的ans,即最靠左或最靠右的1的位置 更新答案 有spj #include<cstdio> using nam ...
- 【Jenkins】Jenkins的安装与配置
一.环境准备 1.下载jdk 官方下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133 ...
- luogu2893 [USACO08FEB]修路Making the Grade
ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...