LeetCode.922- parity array is sorted according to II (Sort Array By Parity II)

This is the first book of pleasure and delight 354 update, the first 379 Pian original

01 questions and look ready

Introduced today is the LeetCodearithmetic problem in Easythe first-level 216title (overall title number is 922). Given array A nonnegative integer, the integer is an odd number in half A, and the remaining half is even.

Sort the array, so that each time the A [i] is an odd number, I is an odd number; A [i] is an even number, I is an even number.
You can return any answers to meet an array of conditions. E.g:

Input: [4,2,5,7]
Output: [4,5,2,7]
Description: [4,7,2,5], [2,5,4,7], [2,7, 4,5] It will also be accepted.

Note :

  • 2 <= A.length <= 20000

  • A.length%2 == 0

  • 0 <= A [i] <= 1000

02 The first solution

Two Listodd, respectively even-save up to create a new array result, if the index is odd, odd on the storage Listmedium as the value of the new array element, contrary to the even-numbered memory Listin value as the new array element .

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

public int[] sortArrayByParityII(int[] A) {
    List<Integer> odd = new ArrayList<Integer>();
    List<Integer> even = new ArrayList<Integer>();
    for (int num : A) {
        if (num%2 == 0) {
            even.add(num);
        } else {
            odd.add(num);
        }
    }
    int j = 0, k = 0;
    int[] result = new int[A.length];
    for (int i=0; i<result.length; i++) {
        if (i%2 == 0) {
            result[i] = even.get(j++);
        } else {
            result[i] = odd.get(k++);
        }
    }
    return result;
}


03 The second solution

We can also directly from Athe values, the same is to create a new resultarray to resultarray two new index, starting from a 0, only the even indices, the other starting from 1, only odd indices, twice traverse the array A, the index values and corresponding elements stored resultin.

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

public int[] sortArrayByParityII2(int[] A) {
    int[] result = new int[A.length];
    int j = 0;
    for (int i=0; i<A.length; i++) {
        if (A[i]%2 == 0) {
            result[j] = A[i];
            j += 2;
        }
    }
    int k = 1;
    for (int i=0; i<A.length; i++) {
        if (A[i]%2 != 0) {
            result[k] = A[i];
            k += 2;
        }
    }
    return result;
}


04 A third solution

The second solution for the above, we can also use only one cycle.

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

public int[] sortArrayByParityII3(int[] A) {
    int[] result = new int[A.length];
    int j = 0, k = 1;
    for (int i=0; i<A.length; i++) {
        if (A[i]%2 == 0) {
            result[j] = A[i];
            j += 2;
        } else {
            result[k] = A[i];
            k += 2;
        }
    }
    return result;
}


05 The fourth solution

Double pointer.

Define two pointers i and j, i representative of even index, starting with zero; J on behalf of the odd-indexed, beginning from n-1 (n is the length of the array A), if the index position corresponding to the even-odd elements, and the odd index positions corresponding element is an even number, the exchange proceeds element. If the index position corresponding to the even-numbered elements is an even number, the index i is incremented even number 2, similarly, the index position corresponding to the odd-odd elements, an odd index j is decremented by 2, the end of cycle condition j i is not less than 1 or less than n.

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

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


06 Summary

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