class Solution {
public:
int maxLength(vector<int>& arr) {
unordered_set<int> visited;
int start = 0, end = 0;
int ret = 0;
while (end < arr.size()) {
if (visited.count(arr[end]) == 0)
visited.insert(arr[end]);
else {
while (arr[start] != arr[end]) {
visited.erase(arr[start]);
++start;
}
++start;
}
++end;
ret = max(ret, end - start);
}
return ret;
}
};
easy版滑动窗口
京公网安备 11010502036488号