第一次做题思路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. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  2. javaweb06 文件的下载

    1. 如何修改小工具或框架的源代码 ? 1). 原则: 能不修改就不修改. 2). 修改的方法: > 修改源代码, 替换 jar 包中对应的 class 文件. > 在本地新建相同的包, ...

  3. MVC——EF 回顾总结

    回顾一下MVC的知识点. 其实开始 我在学校的知识对MVC 还是很模糊的一个概念.只是记得结合EasyUI 增删改查 和分页,代码都是模糊的 进过这段时间的学习,让我对MVC 有了一个很清楚的认识. ...

  4. EditText制作简单的登录界面

    EditText与之前的TextView和Button的用法大体相同,用法案例如下: activity_edit_text.xml: <?xml version="1.0" ...

  5. java 的HashMap底层数据结构

    HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...

  6. PAT Advanced 1029 Median (25) [two pointers]

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  7. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...

  8. P1781 宇宙总统

    题目地址:https://www.luogu.com.cn/problem/P1781 题目描述:地球历公元 6036 年,全宇宙准备竞选一个最贤能的人当总统,共有 n 个非凡拔尖的人竞选总统,现在票 ...

  9. 2017年3月16工作日志【mysql更改字段参数、java8 map()调用方法示例】

    修改某个表的字段类型及指定为空或非空 >alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空],变更字段名称及属性 >alter table 表名称 ...

  10. gitlab安装教程

    gitlab安装教程     安装教程 官网安装方法 https://about.gitlab.com/downloads/#centos7 1.准备 sudo yum install curl po ...