The sword refers to the second edition of the offer interview question 21: adjust the order of the array so that the odd numbers are in front of the even numbers (java)

Topic description:
Input an array of integers and 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 and all even numbers are in the second half of the array.

Basic implementation
If you don't consider the time complexity, the simplest idea should be to scan the array from the beginning, every time you encounter an even number, take out the number, and move all the numbers behind the number to the front. After moving, there is a vacancy at the end of the array, and then put the even number into this vacancy. Since it takes O(n) numbers to move without hitting an even number, the total time complexity is O(n2). However, this approach does not satisfy the interviewer. But if we can say the solution right after hearing the question, the interviewer will at least feel that our thinking is very sensitive.

The solution that only completes the basic functions is only suitable for junior programmers.
This problem requires that the odd numbers be placed in the first half of the array, and the even numbers are placed in the second half of the array, so all odd numbers should be in front of the even numbers, which means that we are scanning In this array, if we find that there are even numbers in front of odd numbers, we can exchange their numbers, and the exchange will meet the requirements.
So we can maintain two pointers, the first pointer is initialized to point to the first number of the array and it only moves backwards; the second pointer is initialized to point to the last number of the array and it points to move forward. The first pointer is always in front of the second pointer until the two pointers meet. If the number pointed to by the first pointer is even and the number pointed to by the second pointer is odd, we swap the two numbers.
write picture description here

Consider the scalability solution.
If you are interviewing fresh graduates or programmers who have not worked for a long time, the interviewer will be satisfied with the previous code, but if the applicant applies for a senior development position, the interviewer may continue Ask a few questions.

Interviewer: If you change the question to the number in the array divided into two parts according to size, all negative numbers are in front of all non-negative numbers, what should you do?

If the title is changed again, the numbers in the array are divided into two parts, and the numbers that are divisible by 3 are in front of the numbers that are not divisible by 3. What should I do?

This is the interviewer looking at our understanding of scalability, that is, we hope that we can give a model, in which we can easily extend the existing solutions to the same type of problems.

code show as below:

/**
 * 调整数组顺序使奇数位于偶数前面
 */
public class OddEventArray {

    public void reorderOddEvent(int[] array){
        if(array == null || array.length == 0){
            return ;
        }

        int len = array.length;
        int low = 0;
        int high = len - 1;
        while(low <= high){
            while(!isEvent(array[low]) && low <= len - 1){
                low++;
            }
            while(isEvent(array[high]) && high >= 0){
                high--;
            }
            if(low <= high){
                int temp = array[low];
                array[low] = array[high];
                array[high] = temp;
            }
        }
    }

    //判断是否是偶数,true:偶数;false:奇数
    public boolean isEvent(int num){
        return (num & 1) == 0;
    }

    public static void main(String[] args) {
        OddEventArray test = new OddEventArray();
        int[] array = {2,1};
        System.out.println(Arrays.toString(array));
        test.reorderOddEvent(array);
        System.out.println(Arrays.toString(array));
    }

}

Guess you like

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