第一次做题思路201511092250

1.采用map存储,key为nums[i],value为以nums[i]为结尾的最大递增子序列的长度

2.采用map里面的lower_bounder函数直接找出第一个大于或等于nums[i]的位置,位置ite--,然后遍历前面的数,找出比nums[i]的数里面,长度len最长的,令nums[i]的最大递增子序列的长度为len+1

3.AC时间为148ms

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
map<int, int> m;
int maxLength = 0;
for (int i = 0; i < nums.size(); i++)
{
map<int, int>::iterator ite = m.lower_bound(nums[i]);
if (ite == m.begin())
m[nums[i]] = 1;
else
{
ite--;
int tmpMax = ite->second + 1;
for (; ite != m.begin(); ite--)//寻找比nums[i]小的数,并在这些数里面,找出长度最大的
tmpMax = max(tmpMax, ite->second + 1);
if (ite == m.begin())//寻找比nums[i]小的数,并在这些数里面,找出长度最大的
tmpMax = max(tmpMax, ite->second + 1);
m[nums[i]] = tmpMax;
}
maxLength = max(maxLength, m[nums[i]]);
}
return maxLength;
}
};

Longest Increasing Subsequence (Medium)的更多相关文章

  1. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

  2. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. 65.Longest Increasing Subsequence(最长增长子序列)

    Level:   Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  9. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

随机推荐

  1. 吴裕雄--天生自然MySQL学习笔记:MySQL 元数据

    你可能想知道MySQL以下三种信息: 查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数. 数据库和数据表的信息: 包含了数据库及数据表的结构信息. MySQL服务器信息: ...

  2. Java Properties基础知识总结

    在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似: some_key=some_value #注释描述 还有一种是使用xml文件保存项目的 ...

  3. java反射修改静态方法的值setAccessible

    这几天闲来无事.在网上看了一个题目,相信大家都知道这个题目  static void change(String str){         str="welcome";     ...

  4. jQuery如何给DOM添加ID

    ID每个元素只能有一个,ID名同一页面也不能重复,addID方法是不需要的,和其他属性一样用attr方法就行了, $(singleTarget).attr('id','idName'); 更简单的 $ ...

  5. ajax上传图片到服务器简单操作

    前端: 通过Ajax方式上传文件,使用FormData进行Ajax请求.上传文件或图片时,要求的格式为enctype ="multipart/form-data"(以二进制格式上传 ...

  6. dotnet core 链接mongodb

    导入命名空间 using MongoDB.Bson; using MongoDB.Driver; 测试示例: var client = new MongoClient("mongodb:// ...

  7. 【MySQL参数】- query_cache_type

    MySQL为什么要关闭查询缓存 https://blog.csdn.net/liqfyiyi/article/details/50178591 Query cache的优化方法 https://blo ...

  8. 【模式分解】无损连接&保持函数依赖

    首先引入定义 无损分解指的是对关系模式分解时,原关系模型下任一合法的关系值在分解之后应能通过自然联接运算恢复起来.反之,则称为有损分解. 保持函数依赖的分解指的是对关系分解时,原关系的闭包与分解后关系 ...

  9. [转载]markown语法

    目录 Cmd Markdown 公式指导手册 一.公式使用参考 1.如何插入公式 2.如何输入上下标 3.如何输入括号和分隔符 4.如何输入分数 5.如何输入开方 6.如何输入省略号 7.如何输入矢量 ...

  10. gcc -S xx

    编译器的核心任务是把C程序翻译成机器的汇编语言(assembly language).汇编语言是人类可以阅读的编程语言,也是相当接近实际机器码的语言.由此导致每种 CPU 架构都有不同的汇编语言. 实 ...