题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
#include <unordered_map> class Solution { public: int maxLength(vector<int>& arr) { unordered_map<int, int> hash; int i = 0, j = 0, res = 0; while (j < arr.size()) { if (hash.find(arr[j]) != hash.end()) { res = j - i > res ? j - i : res; i = hash[arr[j]] + 1; j = i; hash.clear(); } else { hash[arr[j]] = i; j++; } } return j - i > res ? j - i : res; } };