300 Longest Increasing Subsequence 最长上升子序列
给出一个无序的整形数组,找到最长上升子序列的长度。
例如,
给出 [10, 9, 2, 5, 3, 7, 101, 18],
最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4。因为可能会有超过一种的最长上升子序列的组合,因此你只需要输出对应的长度即可。
你的算法的时间复杂度应该在 O(n2) 之内。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?
详见:https://leetcode.com/problems/longest-increasing-subsequence/description/
Java实现:
class Solution {
public int lengthOfLIS(int[] nums) {
int n=nums.length;
if(n==0||nums==null){
return 0;
}
int[] dp=new int[n];
Arrays.fill(dp,1);
int res=1;
for(int i=1;i<n;++i){
for(int j=0;j<i;++j){
if(nums[j]<nums[i]&&dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
}
if(res<dp[i]){
res=dp[i];
}
}
}
return res;
}
}
C++实现:
方法一:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> maxLen(size,1);
int res=1;
for(int i=1;i<size;++i)
{
for(int j=0;j<i;++j)
{
if(nums[j]<nums[i]&&maxLen[j]+1>maxLen[i])
{
maxLen[i]=maxLen[j]+1;
}
if(res<maxLen[i])
{
res=maxLen[i];
}
}
}
return res;
}
};
方法二:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> res;
for(int i=0;i<size;++i)
{
auto it=std::lower_bound(res.begin(),res.end(),nums[i]);
if(it==res.end())
{
res.push_back(nums[i]);
}
else
{
*it=nums[i];
}
}
return res.size();
}
};
参考:https://www.cnblogs.com/grandyang/p/4938187.html
300 Longest Increasing Subsequence 最长上升子序列的更多相关文章
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
随机推荐
- NOIP 2010 乌龟棋
P1541 乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行 NN 个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第 NN 格是终点, ...
- Servlet的调试
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/debugging.html: 测试/调试Servlet始终是困难的.Servlets往往涉及大量 ...
- 绿盟NF防火墙系统
http://www.nsfocus.com.cn/ http://www.nsfocus.com.cn/products/details_22_5.html
- MongoDB小结20 - find【查询条件$size】
size可以获得指定数组长度的文档 db.user.find({"fruit":{"$size":3}},{"_id":0}) { &quo ...
- react 项目实战(九)登录与身份认证
SPA的鉴权方式和传统的web应用不同:由于页面的渲染不再依赖服务端,与服务端的交互都通过接口来完成,而REASTful风格的接口提倡无状态(state less),通常不使用cookie和sessi ...
- RAD 极速应用开发 Spring ROO 入门样例
官网 http://projects.spring.io/spring-roo/ Spring ROO in action ...
- Cocos2d-x 脚本语言Lua基本数据结构-表(table)
Cocos2d-x 脚本语言Lua基本数据结构-表(table) table是Lua中唯一的数据结构.其它语言所提供的数据结构,如:arrays.records.lists.queues.sets等. ...
- CF:Problem 427C - Checkposts强连通 Tarjan算法
tarjan算法第一题 喷我一脸. ...把手写栈的类型开成了BOOL.一直在找错.. . #include<cstdio> #include<cstring> #includ ...
- CCBPM工作流引擎的消息机制与设计
keyword:ccflowjflow 消息机制流程引擎 自己主动发送短信 发送邮件 发送消息 流程引擎微信连接 消息接口 关于ccbpm: 我们把ccflow jflow两个版本号的工作流引擎统称为 ...
- 协议解析Bug分析
协议解析Bug分析 源自邮件协议RPC(远程过程调用)处理的Request请求数据包的bug. 一.Bug描写叙述 腾讯收购的Foxmailclient能够作为outlookclient ...