Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.
Example
Given [1,1,2,3,3,3,2,2,4,1] return 4
public int singleNumberII(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
Arrays.sort(A);
int i = 0;
while (i < A.length - 1) {
if (A[i] == A[i + 1]) {
i += 3;
} else {
break;
}
}
return A[i];
}
思路:
有序排列,相等的数相邻,一次跳3个。