思路
维护一个 v 数组,v [ i ]表示长度为i+1的LIS结尾元素的最小值
class Solution { public: int lengthOfLIS(vector<int>& nums) { if(nums.size() < 2) return nums.size(); vector<int> v; v.push_back(nums[0]); for(int i=1; i<nums.size(); i++) { if(nums[i] < v[0]) v[0] = nums[i]; else if(nums[i] > v[v.size()-1]) v.push_back(nums[i]); else { int l = 0; int r = v.size()-1; int mid; int is_eq = 0; while(l < r) //找到num[i]比v中元素小的第一个位置 { mid = (l + r) / 2; if(nums[i] == v[mid]) { is_eq = 1; break; } else if(nums[i] < v[mid]) r = mid; else l = mid + 1; } if(is_eq == 0 && v[r] > nums[i]) v[r] = nums[i]; } cout<<v.size()<<" "; } return v.size(); } };