class Solution {
public:
int MLS(vector<int>& arr) {
// write code here
sort(arr.begin(), arr.end());
auto it = unique(arr.begin(), arr.end());
arr.erase(it, arr.end());
//注意一定要先排序再去重,不然达不到去重目的
//不要问我怎么知道,卡半天了,有兴趣可以自己试验一下
int right = 1, left = 0;
int sz = arr.size();
if (sz == 1)
return 1;
int len = 1;
while (right < sz) {
if (arr[right - 1] + 1 == arr[right])
len = max(len, right - left + 1);//更新答案
else
left = right;//更新left
right++;
}
return len;
}
};

京公网安备 11010502036488号