class Solution {
public:
int totalFruit(vector<int>& s) {
int ans = 0;
int ln=s[0],rn=s[0];
for(int i =0,j =0;i<s.size();i++)
{
if(s[i]==ln||s[i]==rn) ans = max(ans,i-j+1); //相等继续往后走
else
{
//ans = i - j + 1;
j = i-1; //先移到后面一格
ln = s[j];
while(j>=1&&s[j-1]==ln)j--; //找到最终的j
rn = s[i];
ans = max(ans,i-j+1);
}
}
return ans;
}
};