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