题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// 一看就是滑动窗口啊,知道的话就直接降为简单题了,非常的简单。
unordered_set<int> set_;
int left=0;
int right=0;
int n=arr.size();
int count=0;
while(right<n)
{
if(set_.count(arr[right]))
{
set_.erase(arr[left]);
left++;
}else
{
set_.insert(arr[right]);
if(count<right-left+1)
count=right-left+1;
right++;
}
}
return count;
}
};
想到滑动窗口的思想就非常的简单了,想不到那感觉就复杂了。

