码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]H-Index II

时间:2015-10-02 18:39:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. 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 };

 

 

 

 

 

 

[LeetCode][JavaScript]H-Index II

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4852477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!