题解 | #最长无重复子数组#

最长无重复子数组

http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4

使用双指针检索,map记录数据。

class Solution {
public:
/**
 * 
 * @param arr int整型vector the array
 * @return int整型
 */
int maxLength(vector<int>& arr) {
    int left = 0;
    int right = 0;
    int maxValue = 0;
    map<int, int> dic;
    while (right < arr.size()) {
        int value = arr[right];
        dic[value] = dic[value] + 1;
        if (dic[value] > 1) {
            while (left <= right) {
                int value1 = arr[left];
                dic[value1] = dic[value1] - 1;
                left ++;
                if (dic[value1] == 1) {
                    break;
                }
                
            }
        } else {
            if (right - left + 1 > maxValue) {
                maxValue = right - left + 1;
            }
        }
        right ++;
    }
    return maxValue;
}
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务