"Sword refers to offer" eight, adjust the array order so that the odd digit is preceded by even numbers

Source: Internet
Author: User

The title description enters an array of integers, implements a function to adjust 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. Analysis: The problem is added when practicing on OJ, that is, the order of the adjusted numbers is constant. At present, only one method can be thought of, followed by other methods to add. Create a new array, scan the original array two times, and first remove all the odd numbers from the original array and deposit them in the new array. The second time finds all the even numbers in the new array. The code is implemented as follows:
1  Public classSolution {2      Public voidReorderarray (int[] Array) {3         intLength =Array.Length;4         int[] temp =New int[length];5         if(length==1| | length==0| | array==NULL){6             return ;7         }8         intj = 0 ;9          for(inti = 0; i<length; i++){Ten             if((array[i]&1) ==1){ OneTEMP[J] =Array[i]; AJ + + ; -             } -         } the          for(inti = 0; i<length; i++){ -             if((Array[i] & 1) ==0){ -TEMP[J] =Array[i]; -J + + ; +             } -         } +          for(inti = 0; i<length; i++){ Aarray[i]=Temp[i]; at         } -           -     } -}

"Sword refers to offer" eight, adjust the array order so that the odd digit is 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.