leetcode 300 : Longest Increasing Subsequence
2015-11-06 13:16
344 查看
1、原题如下:
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
2、解题如下:
3、 总结
本题思路:对于任意前i个数据,它的LIS(i)=1+max{LIS(j)},其中j小于且nums[j]小于nums[i];于是双循环遍历即可。
本题还有nlogn复杂度的方法,后续更新~
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
2、解题如下:
class Solution { public: int lengthOfLIS(vector<int>& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return 1; vector<int> tmp(nums.size(),1); int result=1; for(int i=1;i<nums.size();i++) { for(int j=0;j<i;j++) { if(nums[j]<nums[i]) tmp[i]=max(tmp[i],tmp[j]+1); } result=max(result,tmp[i]); } return result; } };
3、 总结
本题思路:对于任意前i个数据,它的LIS(i)=1+max{LIS(j)},其中j小于且nums[j]小于nums[i];于是双循环遍历即可。
本题还有nlogn复杂度的方法,后续更新~
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- 只有程序员看的懂的面试圣经|如何拿下编程面试
- 下一次技术面试时要问的 3 个重要问题
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- PHP程序员面试 切忌急功近利(更需要注重以后的发展)
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例