题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
#include <unordered_map>
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
// write code here
unordered_map<int, int> m;
int left = 0;
int ans = 0;
for(int right = 0;right<arr.size();++right)
{
m[arr[right]]++;
while(m[arr[right]] >= 2)
m[arr[left++]]--;
ans = max(ans,right-left+1);
}
return ans;
}
};
解题思路:滑动窗口+双指针