使用双指针检索,map记录数据。
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
int left = 0;
int right = 0;
int maxValue = 0;
map<int, int> dic;
while (right < arr.size()) {
int value = arr[right];
dic[value] = dic[value] + 1;
if (dic[value] > 1) {
while (left <= right) {
int value1 = arr[left];
dic[value1] = dic[value1] - 1;
left ++;
if (dic[value1] == 1) {
break;
}
}
} else {
if (right - left + 1 > maxValue) {
maxValue = right - left + 1;
}
}
right ++;
}
return maxValue;
}
};