题目描述
给定一个数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同。
子数组是连续的,比如[1,2,3,4,5]的子数组有[1,2],[2,3,4]等等,但是[1,3,4]不是子数组

#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int maxLength(vector<int> & arr)
    {
        int maxlen = 0;
        set<int> st;
        int i = 0, j = 0;
        while( j < arr.size())
        {
            while(st.count(arr[j]))
            {
                st.erase(arr[i++]);
            }
            st.insert(arr[j++]);
            maxlen = max(maxlen, j - i);
        }
        return maxlen;

    }
};

int main()
{
    vector <int> arr = {2,2,3,4,3};
    Solution solution;
    cout << solution.maxLength(arr) << endl;
    system("pause");
}

相关文章:

  • 2021-12-29
  • 2021-06-16
  • 2021-05-18
  • 2021-10-21
  • 2021-06-04
  • 2022-12-23
  • 2021-08-25
猜你喜欢
  • 2022-02-15
  • 2021-08-13
  • 2021-06-11
  • 2021-11-09
  • 2022-12-23
  • 2021-12-08
  • 2021-07-31
相关资源
相似解决方案