题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
模仿ACwing上的查找最长不重复子串的写法,简易哈希
class Solution {
public:
int maxLength(vector<int>& arr) {
if(arr.size() <= 1)
return arr.size();
int used[100010] = {0};
int ans = 0;
for(int i = 0,j = 0;i < arr.size();i++)
{
used[arr[i]]++;
while(j < i && used[arr[i]] > 1)
used[arr[j++]]--;
ans = max(ans,i - j + 1);
}
return ans;
}
};