#include <iostream>
using namespace std;
#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
int bestStartIndex = 0;
int bestLen = len;
int lastStartIndex = 0;
for ( int i=0; i<len; ++i) {
if (!colors[arr[i]])
colorsCounter++;
colors[arr[i]]++;
if (colorsCounter==colorsNeed) {
int j = lastStartIndex;
while (colors[arr[j]]>1) {
colors[arr[j]]--;
++j;
}
if (i-j+1<bestLen) {
bestStartIndex = j;
bestLen = i-j+1;
if (bestLen==colorsNeed)
break;
}
lastStartIndex = j;
}
}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1843505
原文:http://10541556.blog.51cto.com/10531556/1843505