Prove safety Offer- array - adjust the order of the array so that in front of the even-odd

topic

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

Thinking

  • Two empty array during traversal receive odd and even, and finally merge the two arrays

Code

function reOrderArray(array) {
    let arr1 = [];
    let arr2 = [];
    for (let i = 0;i < array.length - 1;i++) {
        if (array[i] % 2 === 1) {
            arr1.push(array[i]);
        }
        if (array[i] % 2 === 0) {
            arr2.push(array[i]);
        }
    }
    array = [...arr1,...arr2];
    return array;
}

Guess you like

Origin www.cnblogs.com/kbinblog/p/11542155.html