Binary Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public int findDuplicate(int[] nums) { int left = 1, right = nums.length - 1;
while (left < right) { int mid = (left + right) >>> 1;
int count = 0; for (int num : nums) { if (num <= mid) { count++; } }
if (count <= mid) { left = mid + 1; } else { right = mid; } }
return left; } }
|
Bit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public int findDuplicate(int[] nums) { int duplicate = 0;
int n = nums.length - 1; int bits = Integer.SIZE - Integer.numberOfLeadingZeros(n);
for (int i = 0; i < bits; i++) { int mask = 1 << i; int bitCountInNums = 0, bitCountInN = 0; for (int j = 0; j < nums.length; j++) { if ((nums[j] & mask) != 0) { bitCountInNums++; } if (j > 0 && (j & mask) != 0) { bitCountInN++; } } if (bitCountInNums > bitCountInN) { duplicate |= mask; } }
return duplicate; } }
|
Two Pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int findDuplicate(int[] nums) { int slow = 0, fast = 0;
while (true) { fast = nums[nums[fast]]; slow = nums[slow];
if (fast == slow) { int curr = 0; while (curr != slow) { curr = nums[curr]; slow = nums[slow]; } return curr; } } } }
|
根据题意,包含 n + 1 个整数的数组 nums,其数字都在 [1, n] 范围内,如 nums = [1, 3, 4, 2, 2],此时共 5 个整数,n = 4,数字都在 [1, 4] 内,那么可将值作为索引,使用环形链表找环入口的解法解题即可。
References
287. Find the Duplicate Number