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 ...
随机推荐
- MySQL查询显示连续的结果
#mysql中 对于查询结果只显示n条连续行的问题# 在领扣上碰到的一个题目:求满足条件的连续3行结果的显示 X city built a new stadium, each day many peo ...
- MySQL 使用GTID进行复制
MySQL 使用GTID进行复制 1. GTID的格式和存储 1.1 GTID 集 1.2 mysql.gtid_executed 表 1.3 mysql.gtid_executed 表压缩 2. G ...
- k8s 基于NFS部署storageclass pv自动供给
在k8s中部署有状态应用时,通常需要做数据持久化存储. 后端存储的方式有以下几种: 1.基于宿主机本地的存储方式: (重启pod时,若pod被调度到其他节点上,尽管原来节点上的数据不会丢失,但是其他节 ...
- 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式
适配器模式:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能在一起工作的 那些类可以在一起工作了[DP] UML类图: 简单模拟一下代码: //已存在的.具有 ...
- solr7.7.1完整教程
安装 上传solr-7.7.1.tgz至服务器 opt文件加下 解压 tar -zxvf solr-7.7.1.tgz 运行 进入到加压后的文件夹/opt/solr-7.7.1,执行一下命令启动sol ...
- machine_desc
每一个machine,都要定义一个自己的machine_desc结构,该结构定义了该machine的一些最基本的特性. struct machine_desc { unsigned int nr; / ...
- studio rendering problems
问题--------> Rendering Problems The following classes could not be instantiated: - android.support ...
- 网页静态化解决方案Freemarker
序言: 沉淀了三个月,逐步将自己最近两年在公司中用到的技术和知识点,重新整理归纳了下,对比以前可以发现,现在技术更新越来越快,也越来越成熟,在互联网企业,用到的技术也更先进,更领先,比如微服务.分布式 ...
- loj2028 「SHOI2016」随机序列
定义区间是内部只含有乘号的区间. 对于区间左端点是 \(l \geq 2\) 的情况,左端点前头是加号的情况和前头是减号的情况的个数是相同的.因此这些区间不对答案产生贡献. 所以区间左端点必定是 \( ...
- java基础语法中容易出错的细节
1 java中的数字默认类型为int **容易出现类型转换错误 long 定义的数字后面必须有 “l” “L” float 定义的数字后面必须有 “f” “F” java中比int表述范围大的数,不会 ...