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个。
本文介绍了一种通过排序和遍历来找出在3n+1个数字中仅出现一次的数字的方法。给定一个包含多个重复三次且仅有一个数字不重复的数组,通过将数组排序并逐个检查元素,可以有效地找到那个只出现一次的数字。
412

被折叠的 条评论
为什么被折叠?



