双指针
what
所谓双指针,是指在遍历对象的过程中,不仅仅普通的使用单个指针去访问,而是用两个相同(或者相反)的指针去扫描。
例题
无重复字符的最长字串
leecode 3
acwing 799
AC code
#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 1e5+10;
int n;
int a[N];
unordered_map<int, int> count;
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int res = 0;
for(int l = 0, r = 0; r < n; r++){
count[a[r]]++;
while(count[a[r]] > 1){//有重复
count[a[l++]]--;
}
res = max(res, r - l + 1);
}
cout << res << endl;
return 0;
}