- 浏览: 183460 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定一个长度为n + 1的数组nums[],里面的元素nums[i]都在1到n之间,只有一个重复元素,让我们找到这个元素,空间复杂度为O(1),时间复杂度不多于O(n^2)。我们可以将数组排序,然后通过二分法找到重复的元素,排序之后,如果中间的元素的值小于或等于中间元素的下标,即nums[m] <= m, 说明重复的元素肯定在m的左边(因为元素的值是从1开始,如果左边没有重复元素,所以中间元素的值肯定会大于中间元素的下标。),让右指针变为m - 1; 否则让左指针变为m+1。这样时间复杂度为O(nlogn)。代码如下:
还有一个很巧妙的方法,只需要O(n)的时间复杂度。我们从第一个元素开始,如果nums[nums[i]]大于0,就把nums[nums[i]]设定为负数,因为只有一个元素重复,当我们往后处理,遇到nums[nums[i]]小于0,说明我们之前访问过这个元素,因此当前元素的值为重复的元素。代码如下:
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.
给定一个长度为n + 1的数组nums[],里面的元素nums[i]都在1到n之间,只有一个重复元素,让我们找到这个元素,空间复杂度为O(1),时间复杂度不多于O(n^2)。我们可以将数组排序,然后通过二分法找到重复的元素,排序之后,如果中间的元素的值小于或等于中间元素的下标,即nums[m] <= m, 说明重复的元素肯定在m的左边(因为元素的值是从1开始,如果左边没有重复元素,所以中间元素的值肯定会大于中间元素的下标。),让右指针变为m - 1; 否则让左指针变为m+1。这样时间复杂度为O(nlogn)。代码如下:
public class Solution { public int findDuplicate(int[] nums) { Arrays.sort(nums); int l = 0; int r = nums.length - 1; while(l < r) { int m = l + (r - l) / 2; if(nums[m] <= m) { r = m - 1; } else { l = m + 1; } } return nums[l]; } }
还有一个很巧妙的方法,只需要O(n)的时间复杂度。我们从第一个元素开始,如果nums[nums[i]]大于0,就把nums[nums[i]]设定为负数,因为只有一个元素重复,当我们往后处理,遇到nums[nums[i]]小于0,说明我们之前访问过这个元素,因此当前元素的值为重复的元素。代码如下:
public class Solution { public int findDuplicate(int[] nums) { int result = 0; for(int i = 0; i < nums.length; i++) { if(nums[Math.abs(nums[i])] > 0) { nums[Math.abs(nums[i])] = - nums[Math.abs(nums[i])]; } else { result = Math.abs(nums[i]); } } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Find the Duplicate Number.java
lru cache leetcode LeetCode copy ...Duplicate Number 指针 快慢指针,链表循环 20200529 198. House Robber 动态规划 如何确定子问题 20200525 20200525 20200525 20200525 20200525 20200525 $1 234
389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](./C++/find-the-difference.cpp) [Python](./Python/find-the-difference.py) | _O(n)_ | _O(1)_ | Easy | | ...
Find the Duplicate Number * 题意:n+1个数属于[1~n],找出重复的那个数 * 难度:Medium * 分类:Array, Two Pointers, Binary Search * 思路:如果nums[i]不在对应位置,则和对应位置交换。如果对应位置上也...
Find the Duplicate Number * 题意:n+1个数属于[1~n],找出重复的那个数 * 难度:Medium * 分类:Array, Two Pointers, Binary Search * 思路:如果nums[i]不在对应位置,则和对应位置交换。如果对应位置上也为该数...
Duplicate Email Remover (Add-... - Don't forget to provide the product registration number: this will allow faster processing of your request. You can contact the technical support service online at ...
27. Creation of a botnet requires an attacker to find vulnerability in some application or system (e.g. exploiting the buffer overflow vulnerability that might exist in an application). After finding ...
However, if we are searching for multiple rows, such as duplicate values, or keys in a range, anything more than a small number of rows will make the nonclustered index search very inefficient. ...
Because some committers work on different parts, the total number in the committers section of the triangle is higher than in the above triangle. The kernel is the main building block of FreeBSD. ...
So a look-up for a key just compares the first keys in the page list to find the page required and gets the key from the page's dictionary. MGIndex is O(log M)+O(1), M being N / PageItemCount ...
This is used by each optimization method to compute the average cost of the population and to count the number of legal (feasible) individuals. This code is the same as Dan Simon. The original one are...
在这个版本中,我们用一个do-while循环来确保生成的每个随机数都是唯一的,通过`find_duplicate()`函数检查新生成的随机数是否已经存在于数组中。 以上两种方法都能实现题目要求的功能,第一种方法简单直接,适用于...
- Handling duplicate values in the original dictionary. 33. **Transform Values** - **Objective:** Apply a transformation function to all values in a dictionary. - **Key Concepts:** - Iterating ...
- **Using PROC UNIVARIATE to Look for Highest and Lowest Values by Percentage**: Demonstrates how to find the highest and lowest values based on a specified percentage. - **Using PROC RANK to Look for...
- Improved memory performance when working with archives containing extremely high number of compressed files. ==================== Version 3.06 Build 2 Made Delphi 2005 compatible Other assorted ...
Added the AcpiGpeCount global that tracks the number of processed GPEs, to be used for debugging systems with a large number of ACPI interrupts. Implemented support for the "DMAR" ACPI table (DMA ...
* When multiple indexes are included with the data from a provider, the IndexDef entries in the TClientDataset will show duplicate ‘Fields‘ and ‘DescFields‘ values (Quality Central 7543)....
The number of significant digits displayed in floating point numbers. ; http://php.net/precision precision = 14 ; Output buffering is a mechanism for controlling how much output data ; (excluding ...
(Find_The_Duplicate_Number) 在不使用额外空间或排序算法的情况下对 0 的 1 的 2 的数组进行排序 (Sort_Colors) 重复和缺失号码 (Repeat_And_Missing_Number) 在没有额外空间的情况下合并两个已排序的数组 (Merge_...