双指针+哈希表
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
unordered_map<int, int> tb;
int left=0, right = 0;
int res = 0;
while(right < arr.size()){
if(tb.find(arr[right]) == tb.end()){
tb[arr[right]] += 1;
right++;
continue;
}
res = max(res, right-left);
while(arr[left] != arr[right] && left < right){
tb.erase(arr[left]);
left++;
}
left++;
right++;
}
res = max(res, right-left);
return res;
}
};