class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
if(arr.empty()){
return 0;
}
set<int> mySet;
int length = 1; //最长无重复子串长度
int left = 0;
int right = 0;
while(left < arr.size() && right < arr.size()){
if(mySet.find(arr[right]) == mySet.end()){
mySet.insert(arr[right++]);
length = max(length,right - left);
}else{
mySet.erase(arr[left++]);
}
}
return length;
}
};
牛客网【最长无重复子数组】
最新推荐文章于 2023-04-22 21:41:05 发布