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

最长无重复子数组

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;

    }
};

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

全部评论

相关推荐

哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
MinJerous:虽然我一直说 计算机不怎么卡学历 但是至少得一本
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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