int totalFruit(int* fruits, int fruitsSize) {
if(fruitsSize<=2) return fruitsSize;
int a[2]={-1,-1};//蓝子空
int max=0,n=1;
int head=0, tail=0;//从head摘到tail
a[0]=fruits[0];
int fad=0,tad=0;
while(head<=tail &&tail<fruitsSize){
tail++;
if(tail==fruitsSize) break;
if(fruits[tail]==a[0]||fruits[tail]==a[1]){
n++;
}else if(a[1]==-1){
tad=tail;
a[1]=fruits[tail];
n++;
}else{
head=tad;
tail=tad;
a[0]=fruits[head];
a[1]=-1;
n=1;
}
if(n>max) max=n;
}
return max;
}
结果:
标签:fruitsSizemax,水果,904,int,max,成篮 From: https://www.cnblogs.com/llllmz/p/18036978