题目:904. 水果成篮
思路:双指针
class Solution {
public:
int totalFruit(vector<int>& fruits) {
int n = fruits.size();
if(n==1) return 1;
int le=0,ri=1;
int res = 0;
while(le<n && ri<n){
int fi = fruits[le];
int se = -1;
while(ri<n){
if(fruits[ri]==fi){
ri++;
}else if(se == -1){
se = fruits[ri];
ri++;
}else if(fruits[ri]==se){
ri++;
}else{
break;
}
}
res = max(res,ri-le);
if(ri>=n) break;
//cout<<le<<" 5 "<<ri<<endl;
int newle = 0;
for(int i=ri-1;i>le;i--){
if(fruits[i]==fruits[ri-1]){
newle = i;
continue;
}else{
newle = i+1;
break;
}
}
le=newle;
ri=le+1;
//cout<<le<<" 4 "<<ri<<endl;
}
return res;
}
};