题目描述:
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.
解题思路:
这题可以采用动态规划的办法,新建一个等长数组count来记录子数组的最长长度,遍历数组nums,第二层依次遍历count数组得到当前元素的最长数组长度.
代码:
1 class Solution { 2 public: 3 int lengthOfLIS(vector<int>& nums) { 4 int ret = 0, n = nums.size(); 5 vector<int> count(n, 1); 6 for(int i = 0; i < n; i++){ 7 for(int j = 0; j < i; j ++){ 8 if(nums[i]>nums[j] && count[j]>=count[i]) 9 //当前元素大于遍历元素 且 遍历元素代表的最长长度+1大于当前元素长度 10 count[i] = count[j]+1; 11 } 12 ret = ret>count[i]?ret:count[i]; //每次得到当前元素的最长长度时与ret进行比较,毕竟当前元素的最长长度不代表所有元素中的最长长度 13 } 14 return ret; 15 } 16 };