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 ...
随机推荐
- Devu and Flowers lucas定理+容斥原理
Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- JSP中HTTP状态码
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/http-status-codes.html: HTTP请求格式和HTTP响应消息的格式一样,都有以下结构 ...
- Mybatis中insert中返回主键ID的方法
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...
- 如何使用python书写守护进程?daemon、python-daemon
可以参考的supervisor实现:https://github.com/Supervisor/supervisor:http://supervisord.org/configuration.html ...
- Linux下完美使用find+grep实现全局代码搜索
作者:zhanhailiang 日期:2014-10-11 背景 在Window下有大量方便的图形化工具能够实现全局搜索,可是Linuxserver中因为使用命令行操作导致全局搜索是一个比較高的门槛. ...
- 【SSO】--单点登录之过滤器(filter)
在单点登录的探索中.用到一个知识点:过滤器(filter).常见的几种验证:Authorization filters,验证用户是否有权限訪问页面:Action Filter,验证用户登录的时候是否用 ...
- java struts jxl 导入导出Excel(无模板)
jar包: import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.io.Fil ...
- 操作JSON对象
JSON类型对象,最简单了,就是键值对,key:value.key:value.一直不停地key:value下去,层层嵌套,理论上多少层都可以,只要你喜欢. 可是,每次应用JSON,我都心烦意乱,甚至 ...
- gcc优化选项解析
1 -fno-defer-pop 函数返回的时候,就立即将栈里面放置的该函数的参数pop出来.这样可以避免函数参数占用过多的栈空间. 2 -fforward-propagate ? 3 -ffp-co ...