题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
int n=arr.size();
if(n<=1){
return n;
}
int maxLength=INT_MIN;
unordered_set<int> m_set;
int left=0,right=0;
while(right<arr.size()){
if(m_set.find(arr[right])==m_set.end()){
m_set.emplace(arr[right]);
right++;
}else{
while(m_set.find(arr[right])!=m_set.end()){
m_set.erase(arr[left]);
left++;
}
m_set.emplace(arr[right]);
right++;
}
maxLength=max(maxLength,right-left);
}
return maxLength;
}
};