滑动窗口
题解 (30条消息) NC41-最长无重复子串-【set、双指针】_菜鸟学编程-CSDN博客
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
set<int> set_count;
int left = 0;
// int right = arr.size();
int right = 0;
int res = 0;
while( left < arr.size() && right < arr.size()){
if( set_count.find(arr[right]) == set_count.end()){
set_count.insert(arr[right]);
right ++;
res = max(res, right - left);
}
else{
set_count.erase(arr[left]);
left++;
}
}
return res;
}
};