题解 | #数组中的最长连续子序列#笨方法:排序、去重、计数
数组中的最长连续子序列
http://www.nowcoder.com/practice/eac1c953170243338f941959146ac4bf
class Solution {
public:
/**
* max increasing subsequence
* @param arr int整型vector the array
* @return int整型
*/
int MLS(vector<int>& arr) {
// write code here
if (arr.size() == 1)
return 1;
int k = 1, maxk = k;
bool pls;
sort(arr.begin(), arr.end());
vector<int>::iterator newit = unique(arr.begin(), arr.end());
arr.erase(newit, arr.end());
for (int i = 0; i < arr.size() - 1; i++)
{
if (arr[i+1] - arr[i] == 1)
pls = true;
else
pls = false;
if (pls)
{
k++;
}
else
{
k = 1;
}
maxk = max(maxk, k);
}
return maxk;
}
};