acwing 801 最长连续不重复子序列(双指针)

i:快指针
j:慢指针
窗口滑动
需要一个计数的数组或者unordered_map
https://www.acwing.com/problem/content/description/801/

#include 

using namespace std;
int a[100005],n,b[100005];

int main()
{
     
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> a[i];
    int res=0;
    for(int i=0,j=0;i<n;i++)
    {
     
        b[a[i]]++;
        while(j<=i&&b[a[i]]>1)
        {
     
            b[a[j]]--;
            j++;
        }
        res=max(res,i-j+1);
    }
    cout << res << endl;
    return 0;
}

你可能感兴趣的:(数据结构)