水果成篮
class Solution {
public:
int totalFruit(vector
<int>& a
) {
int ans
= 1 , n
= a
.size();
int l
=0,r
= l
+1,lr
=l
;
while(l
<n
) {
int x1
= a
[l
],x2
= -1;
while(r
<n
&& (x2
==-1 || a
[r
]==x1
|| a
[r
]==x2
) ){
if(a
[r
]!=x1
&& x2
==-1){
x2
= a
[r
];
}
if(a
[r
] != a
[lr
]){
lr
= r
;
}
r
++;
}
ans
= max(ans
,r
-l
);
if(r
==n
){
break;
}
l
= lr
;
}
return ans
;
}
};
转载请注明原文地址:https://ipadbbs.8miu.com/read-1546.html