Longest Increasing Subsequence (Medium)
第一次做题思路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)的更多相关文章
- [leetcode] 300. Longest Increasing Subsequence (Medium)
题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 65.Longest Increasing Subsequence(最长增长子序列)
Level: Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
随机推荐
- 吴裕雄--天生自然TensorFlow2教程:数学运算
import tensorflow as tf b = tf.fill([2, 2], 2.) a = tf.ones([2, 2]) a+b a-b a*b a/b b // a b % a tf. ...
- Java反射--getDeclaredField()和getField()
Field getField(String name) 返回当前类以及所继承的类的所有public修饰的成员变量 Field getDeclaredField(String name) 返 ...
- 更改php.ini配置
vi /etc/php.ini #编辑修改 @ini_set('memory_limit', '64M'); / ...
- Kafka学习(学习过程记录)
Apache kafka 这,仅是我学习过程中记录的笔记.确定了一个待研究的主题,对这个主题进行全方面的剖析.笔记是用来方便我回顾与学习的,欢迎大家与我进行交流沟通,共同成长.不止是技术. Kafka ...
- day66-CSS伪类选择器和伪元素选择器
1. 伪类选择器:hover 和 focus 比较常用. 1.1 hover:把鼠标移动到内容迈腾2020款TSI DSG舒适型的时候,字体变成了红色. html: <body> < ...
- 完成在本机远程连接HBase进行数据增删改查
1.进行hbase与本机远程连接测试连接 1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字 1.2修 ...
- 嵌入式Linux环境变量如何参与程序运行
1.环境变量一共有两份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR这一份,用户可以用save ...
- Java常用的公共方法
--获取规字符串中的指定名称的某个字段值 1.public String getValueByName(String params,String name) --用于通过表单选中的复选框获取它的值(j ...
- linux复制指定文件
find /somedir -type f|xargs -I {} cp {} . find /somedir -name "*.txt"|xargs -I {} cp {} .
- Filter过滤器的应用
Filter过滤器作用:在每次请求服务资源时做过滤处理. 原理:Filter接口中有一个doFilter方法,当开发人员编写好Filter类实现doFilter方法,并配置对哪个web资源进行拦截后, ...