hello,今天讲解一下双指针算法
双指针本身是一种思想,并不是一种很具体的算法
看几道题
799. 最长连续不重复子序列
Ac代码:
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], st[N];
int maxx;
int main(){
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1, j = 1; i <= n; i ++){
st[a[i]] ++;
while (j < i && st[a[i]] > 1) st[a[j ++]] --;
maxx = max(maxx, i - j + 1);
}
cout << maxx;
return 0;
}
同向指针
本质:把n^2的算法优化到n或2n或max(2m, 2n)
很简单