Prove safety offer13: array [odd, even], even-odd relative positions unchanged.

1. Title Description

  Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

2. The ideas and methods

  array [i]% 2 == 0 using the push_back vector () function is implemented storage. result_odd . INSERT ( result_odd.end (), result_even.begin (), result_even.end () ) // inserted end () before [ result_even.begin (), result_even.end () ] the element interval.

3. C ++ core code

 1 class Solution {
 2 public:
 3     void reOrderArray(vector<int> &array) {
 4         vector<int> result_even, result_odd;
 5         for(int i=0;i<array.size();i++)
 6         {
 7             if(array[i]%2==0){
 8                 result_even.push_back(array[i]);
 9             }
10             else{
11                 result_odd.push_back(array[i]);
12             }
13         }
14         result_odd.insert(result_odd.end(),result_even.begin(),result_even.end());
15         array = result_odd;
16     }
17 };
View Code

 

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11407616.html