LeetCode.905- by the parity array is sorted (Sort Array By Parity)

This is the first book of pleasure and delight 347 update, the first 371 Pian original

01 questions and look ready

Introduced today is the LeetCodearithmetic problem in Easythe first-level 212title (overall title number is 905). To all the odd elements in a given array A a non-negative integer, returns an array of all the even elements consisting of A, A is followed.
You can return any answers to meet an array of conditions. E.g:

Input: [3,1,2,4]
Output: [2,4,3,1]
Description: [4,2,3,1], [2,4,1,3] and [4,2,1 3] is the correct answer.

Note :

  • 1 <= A.length <= 5000

  • 0 <= A[i] <= 5000

02 The first solution

The even elements stored in the array A List, the odd storage elements to List2, create a new array result, and a length Aequal to traverse Listthe front of the even elements into the result, and then traverse List2the odd-even elements followed element .

The time complexity of this solution is the O(N)space complexity Shi O(N).

public int[] sortArrayByParity(int[] A) {
    List<Integer> list = new ArrayList<Integer>();
    List<Integer> list2 = new ArrayList<Integer>();
    int n = A.length;
    for (int i=0; i<n; i++) {
        if (A[i]%2 == 0 ) {
            list.add(A[i]);
        } else {
            list2.add(A[i]);
        }
    }
    int[] result = new int[n];
    int index = 0;
    for (int j=0; j<list.size(); j++) {
        result[index++] = list.get(j);
    }
    for (int j=0; j<list2.size(); j++) {
        result[index++] = list2.get(j);
    }
    return result;
}


03 The second solution

For the first solution, we further simplify, the stored data without using List, we traverse twice the processing element A, even elements as long as the first time, the result is stored in a new array, as long as the second odd elements, deposit into a new array resultin.

The time complexity of this solution is the O(N)space complexity Shi O(N).

public int[] sortArrayByParity2(int[] A) {
    int n = A.length, index = 0;
    int[] result = new int[n];
    for (int i=0; i<n; i++) {
        if (A[i]%2 == 0 ) {
            result[index++] = A[i];
        }
    }
    for (int j=0; j<n; j++) {
        if (A[j]%2 != 0 ) {
            result[index++] = A[j];
        }
    }
    return result;
}


04 A third solution

We can just use a loop, no additional array, with two-pointer to solve problems.

Create two pointers, a start from the first A, denoted by i, the other from the last one A, denoted by j. If a corresponding element i is odd and j is even corresponding element, the two elements need to swap, swap the even-numbered rows in the front to the back of the odd-numbered change. In addition, we need to let two hands move so been traversed all the elements, if the corresponding element i is an even number, jumps to the next element, the same token, if the corresponding element j is odd, to jump to the previous element, until i is not less than j.

The time complexity of this solution is the O(N)space complexity Shi O(1).

public int[] sortArrayByParity3(int[] A) {
    int i = 0, j = A.length-1;
    while (i < j) {
        if (A[i]%2 !=0 && A[j]%2 ==0) {
            int tem = A[j];
            A[j] = A[i];
            A[i] = tem;
        }
        if (A[i]%2 ==0) {
            i++;
        }
        if (A[j]%2 !=0) {
            j--;
        }
    }
    return A;
}


05 Summary

Thematic algorithm has been continuous days more than six months , the algorithm of feature articles 215 + 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/10991148.html