Question 14: Adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

Topic:

Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits are placed in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.

If the constraint is removed: and guaranteed to be odd and odd, the relative position between the even and the even is constant.

Ideas:

If you want to ensure that the relative positions of the odd and odd, even and even numbers are the same, you need to open up new space to hold the odd and even numbers in two ways:

1. Open a new array, iterate through the original array first, write the odd number to the new array sequentially, iterate over the original array, and write the even number to the new array, and return the new array;

2, open two arrays, iterate over the original array, respectively, the odd and even into two new arrays, and then the odd and even arrays are written to the original array, return the original array;

If you do not need to guarantee that the relative positions of the odd and odd, even and even numbers are constant, then you can do this by backward and backwards traversal, the odd-even swap:

Maintain two pointers, point to the end of the array, and then one backward forward, and then swap the two numbers if the first pointer points to an even number before the two pointers meet, and the second pointer points to the odd one. (No need to open up new space)

Similar topics:

    • Change the title to divide the array into two parts, with all negative numbers preceded by non-negative numbers;
    • Change the title to divide the array into two parts, divisible by 3 in front of not divisible by 3;

To make the code more extensible, these criteria can be written separately as a function interface, while the main code framework remains the same.

Code:
#include <iostream> #include <vector>using namespace std;//not changed the relative positionvoid    Reorderoddeven_1 (vector<int> &array) {if (Array.size () <=0) return;    Vector<int> odds;    Vector<int> Evens;    unsigned int i;        For (I=0;i<array.size (); i++) {if ((array[i]&0x1)!=0) Odds.push_back (Array[i]);    else Evens.push_back (Array[i]);    } for (I=0;i<odds.size (); i++) array[i]=odds[i];    int k=i; For (I=0;i<evens.size (); i++) array[k++]=evens[i];}        Changed the relative positionvoid reorderoddeven_2 (int *pdata,unsigned int length) {if (Pdata==null | | length<=0)    Return    int *pbegin=pdata;    int *pend=pdata+length-1;        while (Pbegin<pend) {while (Pbegin<pend && (*pbegin&0x1)!=0) pbegin++;        while (Pbegin<pend && (*pend&0x1) ==0) pend--;        if (pbegin<pend) {int tmp=*pbegin;    *pbegin=*pend;        *pend=tmp; }}}bool IsEven (int n) {return (n&1) ==0;} More expansiblevoid reorderoddeven_3 (int *pdata,unsigned int Length,bool (*FUNC) (int)) {if (Pdata==null | | length<    =0) return;    int *pbegin=pdata;    int *pend=pdata+length-1;        while (Pbegin<pend) {while (Pbegin<pend &&!func (*pbegin)) pbegin++;        while (Pbegin<pend && func (*pend)) pend--;            if (pbegin<pend) {int tmp=*pbegin;            *pbegin=*pend;        *pend=tmp;    }}}int Main () {int a[]={1,2,3,4,5,6,7,8,9};    int length=sizeof (A)/sizeof (a[0]);    Reorderoddeven_2 (a,length);    for (int i=0;i<length;i++) cout<<a[i]<< "";    cout<<endl;    int b[]={1,2,3,4,5,6,7,8,9};    Vector<int> C (b,b+length);    Reorderoddeven_1 (C);    for (int i=0;i<length;i++) cout<<c[i]<< "";    cout<<endl; Reorderoddeven_3 (B,length,iseven);    for (int i=0;i<length;i++) cout<<b[i]<< "";    cout<<endl; return 0;}
Online test OJ:

Http://www.nowcoder.com/books/coding-interviews/beb5aa231adc45b2a5dcc5b62c93f593?rp=1

AC Code:

Class Solution {Public:void Reorderarray (vector<int> &array) {if (Array.size () <=0)            return;        Vector<int> Odds,evens;        unsigned int i;        For (I=0;i<array.size (); i++) {            if ((array[i]&0x1)!=0)               Odds.push_back (Array[i]);            if ((array[i]&0x1) ==0)               Evens.push_back (Array[i]);        }                For (I=0;i<odds.size (); i++)            array[i]=odds[i];        int k=i;        For (I=0;i<evens.size (); i++)            array[k++]=evens[i];};

  

Question 14: Adjust the array order so that the odd digits are preceded by even numbers

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.