Sword Offer_ Find any repeating element in an array

Source: Internet
Author: User

Title Requirements:

 Title one: 
1. There is an array of integers with a length of N+1
2. Find any duplicate number in the array
3. Modify the original array

idea 1: time-wasting algorithm time complexity O (LOGN) space complexity is O (1)
1. Title Description There must be a duplicate number in the array
B. Using a binary lookup method, the total range is divided into two ranges, traversing the entire array, and counting the number of elements of the array element in the left range,
If the number of array elements in the left range is greater than the length of this range, it is stated that the left range must be in the repeating element,
to the left, otherwise to the right range, until the downline equals the upper limit, notice that the output is the upper or lower limit value, The core of this algorithm is based on the size of the element value to find its corresponding home.

Code:

public static int getduplicationcosttime (int[] arrays) {
  int low=1; 
Because the value range of the array element is 1-n
int high=arrays.length-1;
int mid;
while (low int count=0;
Mid= (Low+high)/2;
for (int i=0;i< arrays.length;i++) {
if (low<=arrays[i]&&arrays[i]<=mid) {
count++;
}
}
//If the number of elements within the range is greater than the length of the left range, go to the left range
if (count> (mid-low+1)) {
High=mid;
}else {
low=mid+1;
}
}
return high;
}

Idea two: Wasted Space algorithm:
Open up an auxiliary array, length is to find the length of the array plus one; traverse the original array, copy the element with the value m in the original array to the position labeled M in the secondary array
Each copy is compared to whether the element labeled M in the auxiliary array is equal to the element being inserted, if so, the element is already copied, it is obvious that this element is the repeating element we are looking for
code
public static int Getduplicationcostspace (int[] arrays) {
   Int[] Assistarrays=new int[arrays.length+1];
int k=0;
for (int i=0;i<arrays.length;i++) {
if (Arrays[i]!=assistarrays[arrays[i]]) {
Assistarrays[arrays[i]]=arrays[i];
}else {
K=arrays[i];
Break
}
}
return k;
}


Topic Two:
Requirements: 1. Allow modification of original array
2. Find any duplicate numbers in the array
3.
There is an array of integers of length n that have all the numbers within the 0~n-1 range
Idea: Traversing this array, traversing to the subscript I position, assuming this is the element value is m, to determine whether M and subscript are equal, if equal, indicating that the element m is in its own position,
Otherwise, M is compared to an array element labeled M, and if unequal, the two are exchanged, and if equal, the person element m is a repeating element, output m.

Code:
public static int getduplication (int[] arrays) {
int k=0;
int help;
for (int i = 0; i < arrays.length; i++) {
Current element and subscript are not in season
while (arrays[i]! = i) {
if (arrays[arrays[i]] = = Arrays[i]) {
K=arrays[i];
Indicates that duplicate elements have been found
I=arrays.length-1;
Break
} else {
Help=arrays[i];
Arrays[i]=arrays[arrays[i]];
Arrays[help]=help;
}
}
}
return k;
}




























Sword Offer_ Find any repeating element in an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.