This is the first book of pleasure and delight 365 update, the first 393 Pian original
01 questions and look ready
Introduced today is the LeetCode
arithmetic problem in Easy
the first-level 227
title (overall title number is 961
). The size 2N
of the array A
, there is N+1
a unique element, and a duplicate of those elements N
times.
Returns an element is repeated N times. E.g:
Input: [1,2,3,3]
Output: 3
Input: [2,1,2,5,3,2]
Output: 2
Input: [5,1,5,2,5,3,5,4]
Output: 5
Note :
4 <= A.length <= 10000
0 <= A [i] <10000
A.length is even
02 The first solution
The title is meant to find an array A
appears in N/2
times of number, which N
is the array A
length. Use HashMap
, key
array element, value
its number of occurrences, the first A
element in the initialization into HashMap
, and then traverse HashMap
to find the value
equal N/2
of key
return can be.
public int repeatedNTimes(int[] A) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int n : A) {
map.put(n, map.getOrDefault(n, 0)+1);
}
int half = A.length/2;
for (Integer key : map.keySet()) {
if (map.get(key) == half) {
return key;
}
}
return -1;
}
03 The second solution
The same idea is to count again find the solution of the first HashMap
into int
the array, length 10001, a new index for the count array A
elements, the value of A
the number of occurrences of the elements, and then traverse count
the array, wherein the return value is equal to N/2
index N
array A
length.
public int repeatedNTimes2(int[] A) {
int[] count = new int[10001];
for (int n : A) {
count[n]++;
}
int half = A.length/2;
for (int i=0; i<count.length; i++) {
if (count[i] == half) {
return i;
}
}
return -1;
}
04 A third solution
A different point of view, the array of repeating elements found on the line, try to re-choice HashSet
, traversing A
the elements, if HashSet
already exists in the current element, this element is the element that is looking for multiple occurrences.
public int repeatedNTimes3(int[] A) {
Set<Integer> set = new HashSet<Integer>();
for (int n : A) {
if (set.contains(n)) {
return n;
} else {
set.add(n);
}
}
return -1;
}
05 The fourth solution
A third solution and the idea is the same, just HashSet
replace the int
array.
public int repeatedNTimes4(int[] A) {
int[] count = new int[10001];
for (int n : A) {
if(++count[n] >= 2) {
return n;
}
}
return -1;
}
06 fifth Solution
In a fourth solution on the basis of, for further simplicity, instead of the string. Create a new string str, if the current element is not appeared in str, it spliced to str, str contrary is already present in the element, the element can be returned.
public int repeatedNTimes5(int[] A) {
String str = "";
for (int n : A) {
if (str.indexOf(n+"") < 0) {
str += n;
} else {
return n;
}
}
return -1;
}
07 sixth Solution
Direct use of two cycles, matching equal elements.
public int repeatedNTimes6(int[] A) {
int n = A.length;
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (A[i] == A[j]) {
return A[i];
}
}
}
return -1;
}
08 seventh Solution
This solution from the LeetCode
given parameters A, this idea is wonderful, be further simplified in the sixth solution on the basis of.
Also the use of two-cycle, but not every time a sixth comparison adjacent elements but three times as jumping comparison solution, the first is more neighbors, the second interval is a comparative element, the third interval is relatively elements 2, 4 will be cut into a length of a is a group of sub-arrays, in which the elements of its distance element 1, 2 for comparison, there will be at least one repeating element in them.
public int repeatedNTimes7(int[] A) {
int n = A.length;
for (int i=1; i<=3; i++) {
for (int j=0; j<n-i; j++) {
if (A[j] == A[j+i]) {
return A[i];
}
}
}
return -1;
}
09 Summary
Thematic algorithm has been continuously day and more than seven months , the algorithm of feature articles 233 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .
That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!