"Sword Point offer" nine degrees OJ1516: adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

Title Link Address:
http://ac.jobdu.com/problem.php?pid=1516

Topic 1516: Adjusting the array order so that the odd digits are preceded by even numbers

Time limit: 1 seconds memory limit: 128 trillion special problem: No submit: 2858 Resolution: 924
Topic description Narration:
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.
Input:
Each input file includes a set of test cases.
For each test case. The first line enters an n, which represents the number of digits in the array.
Enter n integers for the next line.

Represents the number of n in an array.
Output:
Each test case,
Enter a row of n digits, representing the adjusted array. Attention. Numbers and numbers are separated by a space, and there are no spaces after the last number.
Example Input:
5
1 2 3) 4 5
Example output:
1 3 5) 2 4

Thinking Analysis:

The question on the offer book is simpler than the sword. There is no requirement that the order be changed. Method (O (n) complexity):

设定两个指向数组的指针,一个初始化数组第一个元素一个初始化最后一个元素,头指针向后扫描偶数。假设发现偶数,则让尾指针向前扫描,找出一个奇数。然后两者交换。

然后继续头指针向后扫描,直到尾指针在头指针的前面。结束。

But the order of the odd and even numbers is disrupted during the exchange. Does not meet the requirements of this topic.
and the nine-degree topic was improved:
The order of adjustment is required. So that all the odd numbers are in front, all the even numbers are behind, and the order of the odd numbers cannot change, the order of even numbers cannot change . That is, "adjust order" and "relative position unchanged".
Method:
  

    1. An array is scanned two times. The odd and even numbers are recorded successively, and then the array is written back to the original array. Both time and space complexity are O (n).
    2. Build two arrays. Record odd and even numbers. The array is then written back to the original array, and both time and space complexity are O (n).

    3. Build two single-linked lists, one to store odd numbers. An even number is stored, and the odd even is written back to the original array. Both time and space complexity are O (n).

Code:

/********************************* "Sword refers to the offer surface question" Nine degrees OJ1516: adjust the array order so that the odd number is in front of even author: pastoral, date:2015 years Email:[email Protected] **********************************/ #include <stdio.h>#include <stdlib.h>#include <string>#include <math.h>#include <stack>#include <vector>#include <iostream>using namespace STD;intMain () { vector<int>Odd, even;intn, I, num; while(scanf("%d", &n)! = EOF) {odd.clear (); Even.clear (); for(i =0; I < n; ++i) {scanf("%d", &num); Num &1?        Odd.push_back (num): Even.push_back (num); } for(i =0; I < even.size ();        ++i) {odd.push_back (even[i]); }printf("%d", odd[0]); for(i =1; I < odd.size (); ++i) {printf("%d", Odd[i]); }printf("\ n"); }return 0;}/************************************************************** problem:1516 language:c++ result:accepted TIME:80 Ms memory:2552 kb****************************************************************/

Summarize:

    • Use two pointers
    • Good at opening up array assist

"Sword refers to the offer surface question" Nine degrees OJ1516: adjust the array order so that the odd number 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.