Reorder array so odd numbers come before even numbers

describe

Input an array of integers, implement a function to adjust the order of the numbers in the array so that all odd numbers are in the first half of the array, all even numbers are in the second half of the array, and the relative relationship between odd and odd, even and even numbers is guaranteed The location does not change.

Idea: Traverse the array twice, the first time only take odd numbers, the second time only even numbers.

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] reOrderArray (int[] array) {
        // write code here
        int[] res = new int[array.length];
        int temp = 0;
        for(int i=0;i<array.length;i++){
            if(array[i]%2==1){
                res[temp++] = array[i];
            }
        }
        for(int i=0;i<array.length;i++){
            if(array[i]%2==0){
                res[temp++] = array[i];
            }
        }
        return res;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324162287&siteId=291194637