题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
//C++ 滑动窗口实现
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
map<int,int> m;
int left=0,right=0;
int length=0;
int lens=arr.size();
while(right<lens){
int temp=arr[right++];
if(m[temp]==0)
{
m[temp]+=1;
length=max(length,right-left);
}else{
while(left<right&&m[temp]==1)
{
int temp_=arr[left++];
m[temp_]-=1;
}
m[temp]+=1;
}
}
return length;
}
};