Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2)
. - There is only one duplicate number in the array, but it could be repeated more than once.
https://leetcode.com/problems/find-the-duplicate-number/
找出数组中重复的元素,每个数组中只有一个重复元素。
难,每次碰到鸽笼原理整个人都不太好。
不能修改数组就不能排序,常数级的空间复杂度不能开哈希表,复杂度还要小于O(n^2)。
最直观的想法就是一个个看是不是重复,这样复杂度是O(n^2)。
使用二分法来降低复杂度到O(nlogn),那要怎么知道结果是在前半还是后半?
根据鸽笼原理:如果有n+1个(或者多于n+1个)数大于等于n,那肯定结果在后半,反之在前半。
https://leetcode.com/discuss/60830/solutions-explanation-space-without-changing-input-array
1 /** 2 * @param {number[]} nums 3 * @return {number} 4 */ 5 var findDuplicate = function(nums) { 6 var start = 1, end = nums.length - 1, middle, count, i; 7 while(start < end){ 8 count = 0; 9 middle = parseInt((start + end) / 2); 10 for(i = 0; i < nums.length; i++){ 11 if(nums[i] <= middle){ 12 count++; 13 } 14 } 15 if(count <= middle){ 16 start = middle + 1; 17 }else{ 18 end = middle; 19 } 20 } 21 return start; 22 };
时间: 2024-10-11 03:53:41