[LeetCode][JavaScript]H-Index II

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 };
时间: 2024-10-11 03:53:41

[LeetCode][JavaScript]H-Index II的相关文章

[LeetCode][JavaScript]Combination Sum II

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

[LeetCode][JavaScript]Single Number II

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

[LeetCode][JavaScript]Unique Paths II

Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obsta

[LeetCode][JavaScript]Spiral Matrix II

Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] https://leetcode.com/problems/

[LeetCode][JavaScript]Path Sum II

Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] https://leetco

[LeetCode][JavaScript]Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. https://leetcode.com/problems/majority-element-ii/ 使用Moore voting algorithm,时间复杂度是

[LeetCode][JavaScript]Contains Duplicate II

Contains Duplicate II Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. https://leetcode.com/proble

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us