最长无的重复子串的长度

题目描述

给定一个数组arr,返回arr的最长无的重复子串的长度(无重复指的是所有数字都不相同)。

备注:1 <= n <= 105

使用空间换时间

int maxLength(vector<int>& arr)
{
    int size = arr.size();
    int check[1000000] = { 0 };
    int left = 0;
    int right = 0;
    int max = 1;
    for (left = 0; left < size; left++)
    {
        memset(check, 0, sizeof(check));
        check[arr[left]] = 1;

        right = left + 1;
        while (right < size)
        {
            if (check[arr[right]] == 0)
            {
                int len = right - left + 1;
                max = max > len ? max : len;
                check[arr[right]] = 1;
            }
            else
            {
                break;
            }
            right++;
        }
    }

    //cout << max << endl;
    return max;
}

 

posted @ 2021-04-08 21:23  唯一诺  阅读(145)  评论(0)    收藏  举报