可以解决一个整数数组中, 每个数字重复N次,只有一个数字重复了少于N次(比如M次)的问题, 可以构造一个满N清零的迭代器,时间复杂度为O(N),空间复杂度为O(1)。
public void counts(int [] A){
int ones = 0, twos = 0, threes = 0, fours = 0;
for(int i=0; i<A.length; i++){
fours = 0;
fours |= threes & A[i];
threes |= twos & A[i];
twos |= ones & A[i];
ones |= A[i];
ones &= ~twos;
ones &= ~threes;
twos &= ~threes;
ones &= ~fours;
threes &= ~fours;
}
System.out.println(ones);
System.out.println(twos);
System.out.println(threes);
System.out.println(fours);
}