The sword refers to the offer------(sort, quick idea)---Adjust the array order so that the odd digits precede the even number

Source: Internet
Author: User

Tag:--    Code    class   array   art    length   ole   ret   details   

https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&rp=1&ru=% 2fta%2fcoding-interviews&qru=%2fta%2fcoding-interviews%2fquestion-ranking&tpage=1   Test Instructions title: 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. The relative position does not change.   Analysis http://blog.csdn.net/lks1139230294/article/details/52761479 1: Because the even number is put on the last side, so an even number appears at the end, Moving the rest of the part forward at the same time will ensure that the relative position does not change. O (n^2) Idea 2: First to find an even, even to the back of it, so from the even start to find the odd number, found the odd number ahead of the even position, because the odd number found is the first odd number after the even, so the odd and even between the two must be moved after one.    Code: public class Solution {    public void Reorderarray (int [] array) {         if (array = = NULL | | array.length <= 1) return;     & Nbsp;          int first = 0;         int second = 0;       //This side why use while without for, because it is not step by step, but can be directly to the end. The while is used for continuous post-move operations.       &Nbsp; while (First < Array.Length) {             while (First < Array.Length && isodd (Array[first])) {                 first ++;             }            if (First >= ARRAY.LENGTH-1) {                 break;            }             second = First + 1;             while (Second < Array.Length &&!isodd (Array[second])) {                 second ++;             }             if (Second > Array.length-1) {                 break;            }             int temp = array[second];             for (int i = second-1; I >= first; i--) {&NBSP;&NBSP;&NB Sp;             array[i + 1] = array[i];             }             array[first] = temp;        }     }        public boolean isodd (int num) {  &nbsP;     return (num & 1) = = 1;    }}

Sword refers to offer------(sort, quick idea)---Adjust the array order so that the odd digits precede the even number

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.