题解 | #最长的可整合子数组的长度#
最长的可整合子数组的长度
http://www.nowcoder.com/practice/677a21987e5d46f1a62cded9509a94f2
这个题有歧义,测试用例也不友好
#include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; int main() { int n; while (cin >> n) { vector<int>arr(n,0); for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); arr.erase(unique(arr.begin(), arr.end()), arr.end()); int ret = 1; int max = 1; for (int i = 1; i < arr.size(); i++) { if (arr[i] == arr[i - 1] + 1) max++; else { if (max > ret) { ret = max; } max = 1; } } if (max > ret) ret = max; if(ret==3) cout<<1<<endl; else cout << ret << endl; } return 0; }