LeetCode.961-2N repetitions N array elements (N-Repeated Element in Size 2N Array)

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 LeetCodearithmetic problem in Easythe first-level 227title (overall title number is 961). The size 2Nof the array A, there is N+1a unique element, and a duplicate of those elements Ntimes.

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 Aappears in N/2times of number, which Nis the array Alength. Use HashMap, keyarray element, valueits number of occurrences, the first Aelement in the initialization into HashMap, and then traverse HashMapto find the valueequal N/2of keyreturn 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 HashMapinto intthe array, length 10001, a new index for the count array Aelements, the value of Athe number of occurrences of the elements, and then traverse countthe array, wherein the return value is equal to N/2index Narray Alength.

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 Athe elements, if HashSetalready 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 HashSetreplace the intarray.

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 LeetCodegiven 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!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/11094905.html