题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4?tpId=295&tqId=1008889&ru=%2Fpractice%2Fc3a6afee325e472386a1c4eb1ef987f3&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj
class Solution {
public:
/**
*
* @param arr int整型vector the array
* @return int整型
*/
int maxLength(vector<int>& arr) {
int res = 0;
std::unordered_map<int, int> hash;
for (int left = 0, right = 0; right < arr.size(); ++right) {
++hash[arr[right]];
while (hash[arr[right]] > 1) {
--hash[arr[left++]];
}
res = std::max(res, right - left + 1);
}
return res;
}
};