滑动窗口
public static int subarraysWithKDistinct(int[] A, int K) {
if (A==null||A.length<K) return 0;
int[] hash=new int[A.length+1];
int l=0,result=1,count=0;
int results=0;
for (int r=0;r<A.length;r++){
hash[A[r]]++;
if (hash[A[r]]==1) count++;
while (count>K||hash[A[l]]>1){
if (count>K){
result=1;
count--;
}else {
result++;
}
hash[A[l]]--;
l++;
}
if (count==K) results+=result;
}
return results;
}