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

最长无重复子数组

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

class Solution {
public:
    /**
     * 
     * @param arr int整型vector the array
     * @return int整型
     */
    int maxLength(vector<int>& arr) {
        // 一看就是滑动窗口啊,知道的话就直接降为简单题了,非常的简单。
        unordered_set<int> set_;
        int left=0;
        int right=0;
        int n=arr.size();
        int count=0;
        while(right<n)
        {
            if(set_.count(arr[right]))
            {
                set_.erase(arr[left]);
                left++;
            }else
            {
                set_.insert(arr[right]);
                if(count<right-left+1)
                count=right-left+1;
                right++;
            }
        }
        return count;

    }
};

想到滑动窗口的思想就非常的简单了,想不到那感觉就复杂了。

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:20
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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