Offer21----An odd number of even numbers in an array

Source: Internet
Author: User

Title Description:
Enter an array of integers to implement a function to adjust the order of the numbers in the array so that all the odd digits are in the first half of the array, and all the even digits are in the second half of the array.

Basic implementation
Without considering the complexity of the time, the simplest idea would be to scan the array from scratch, take out this number each time an even number is encountered, and move all the numbers that are behind that number to the front. After you have moved, you have an empty space at the end of the array, and then put the even number in the empty space. The total time complexity is O (N2), since it is necessary to move o (n) numbers without touching an even number. However, this method does not satisfy the interviewer. But if we can say this solution immediately after we hear the question, the interviewer will at least feel that our thinking is very sensitive.

Only the basic function of the solution, only for the novice programmer
This topic requires the odd number to be placed in the first half of the array, even in the second half of the array, so that all the odd numbers should be in front of even numbers, that is, when we scan the array, if we find that there are even digits in front of the odd, we can exchange their order, and then meet the requirements.
So we can maintain two pointers, the first pointer to the first digit of the array, it moves backwards, and the second pointer initializes to the last digit of the array, which refers to moving forward. Before the two pointers meet, the first pointer is always in front of the second pointer. If the number of the first pointer is even, and the second pointer points to an odd number, we exchange two digits.

Consider the solution of extensibility, second-kill offer
If you are interviewing a recent graduate or a programmer who is not working long hours, the interviewer will be happy with the code above, but if the candidate is applying for a senior development position, the interviewer may ask a few questions.

Interviewer: If the number of questions in the group is divided into two parts according to the size, all negative numbers in front of all non-negative, what should be done?

If you change the title to change the number of the array into two parts, can be divisible by 3 of the number is not divisible by 3 in front of the number, how to do?

This is where the interviewer is looking at our understanding of extensibility, which we hope we can give a pattern in which the existing solution can be extended to the same type of problem.

The code is as follows:

public class Offer21 {public static void main (string[] args) {int[] arr = {1, 2, 3, 4, 5, 6, 7};change (arr, Oper_type. Oddeven); for (int i:arr) {System.out.print ("" + i);}
}public static void Change (int[] arr, Oper_type TYPE) {if (Arr.length = = 0 | | arr = null) {return;} int left = 0;int right = Arr.length-1;while (left < right) {//func (type, Arr[left]) while (left < right && (Arr[left] & 1) = = 1) {left++;} !func (type, arr[right]) while (Right > Left && (arr[right] & 1) = = 0) {right--;} int temp = Arr[left];arr[left] = arr[right];arr[right] = temp;}}}

In the above code, although the function has been completed, but according to the interviewer said, one will change to negative positive exchange, a 3 of the number of integers, then we write code is not crazy, every day to change the core source code, so we think, the core source code is not moving (because this problem in addition to judging the condition changes, other can not change) So I made the improvements:

public class Offer21 {public static void main (string[] args) {int[] arr = {1, 2, 3, 4, 5, 6, 7};change (arr, Oper_type. Oddeven); for (int i:arr) {System.out.print ("" + i);} System.out.println (); int[] arr1 = {3, 5, 6, 9, 7,};change (ARR1, Oper_type. three); for (int i:arr1) {System.out.print ("" + i);} System.out.println (); int[] arr2 = { -3, 5,-6, 9,-7,};change (ARR2, Oper_type. Plusorminus); for (int i:arr2) {System.out.print ("" + i);}} public static void Change (int[] arr, Oper_type TYPE) {if (Arr.length = = 0 | | arr = null) {return;} int left = 0;int right = Arr.length-1;while (left < right) {//while (left < right && func (type, arr[left]) ) {left++;} while (right > Left &&!func (type, arr[right])) {right--;} int temp = Arr[left];arr[left] = arr[right];arr[right] = temp;}} Private static Boolean func (oper_type type, int num) {if (TYPE = = null) {return true;} Switch (type) {case Oddeven:return funcoddeven (num), Case Plusorminus:return Plusorminus (num); CAse three:return three (num);d Efault:return true;}} Private static Boolean three (int num) {return num% 3 = = 0? true:false;} private static Boolean plusorminus (int num) {return num < 0? true:false;} private static Boolean funcoddeven (int num) {return (num & 1) = = 1? true:false;}} Define the meaning of the enumeration: Make the feature selection more rigorous enum Oper_type {Oddeven ("parity"), Plusorminus ("positive and negative"), three ("3 divisible");p rivate String Type;oper_type ( String type {this.type = type;} Public String GetType () {return type;}}

In the above code, the Func method has already solved the interviewer's concern, we only need to pass a function option (enumeration), we can let the method know the current intention, and call those packaged methods, so that the program is extensible!

Offer21----An odd number of even numbers in an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.