题解 | #最长无重复子数组#C++ unordered_map 解决
最长无重复子数组
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 // 数组的值——数组的index unordered_map<int, int> value_index; int maxSize = 0; int curSize = 0; for (int i = 0; i < arr.size(); i++) { if (value_index.count(arr[i]) == 0) { value_index[arr[i]] = i; curSize++; } else if (value_index.count(arr[i]) > 0){ // 记录上一次出现重复的值的 index i = value_index[arr[i]]; value_index.clear(); curSize = 0; } if (maxSize < curSize) { maxSize = curSize; } } return maxSize; } };