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

最长无重复子数组

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

#include <unordered_map>
class Solution {
  public:
    int maxLength(vector<int>& arr) {
        unordered_map<int, int> hash;
        int i = 0, j = 0, res = 0;
        while (j < arr.size()) {
            if (hash.find(arr[j]) != hash.end()) {
                res = j - i > res ? j - i : res;
                i = hash[arr[j]] + 1;
                j = i;
                hash.clear();
            } else {
                hash[arr[j]] = i;
                j++;
            }
        }
        return j - i > res ? j - i : res;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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