Bovine offer customer network 13 to prove safety problems - such adjustment order of the array located in front of the even-odd

Topic Source : prove safety offer

Topic :

       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.

Problem-solving ideas:

      Array class topic guidelines: In addition to very fool topics, like most of the array of topics to be used in the "double indexing" strategy, but some use a pointer to hit, and some use follows the pointer.

     We just give an example of the array, it analyzes how to do it more appropriate: 2474358109;

     So this time, how would you do, of course, of course, we need to find out all the odd and all even. Based on this idea of find, of course we can do this: from start to finish of each element determining a parity of the array, and then generates a sequence of odd-numbered original spatial array and the array of the same size, a scanned over the fresh the first half of the array, and then is scanned into the second half of the even put new array, no doubt, but the results of this strategy point of view, there is no problem, the problem is: we use an array of original and the same array of space. This complexity makes the time and space complexity are O (n) can be seen, this is not a good method. Is there a better way to do that?

     Of course, there are:

     Please do not forget that we end up doing is: the odd to the front in accordance with the original order, the even put back in the original sequence. Is it possible, we are only in the original space, data movement can do to achieve this desire it? The answer is yes.

     So a natural question we want to send out is: When will we move this?

     Of course, it is a odd and a front (left) farthest away from him between even the presence of such move. In this case then, obviously it requires two indexes to identify two odd and even. For example the above sequence: 247, we know of course, correct sequence 724. So how should we do it?

Code:

     I give the following core code, you can probably understand the:

. 1              tmp = Array [J]; // odd cached 
2              for ( int K = J; K> I; K-- )
 . 3                  Array [K] = Array [- K- . 1 ];
 . 4              Array [I] = tmp ; // odd placed in the position that it should

 

Clearly, our approach is: 7 cached to a variable, then, will all even moved backwards by one unit, then 7 it should put in place. The core idea of ​​this code is to solve this problem.

But I still encountered such a problem array bounds in the process of debugging:

I put my complete code in below for reference:

 1 class Solution {
 2 public:
 3     void reOrderArray(vector<int> &array) {//tow pointer
 4          int length = array.size();
 5          int i = 0;//奇数索引位置
 6          int j = 0;
 7          int tmp;
 8         if(length > 1)
 9         {
10         while((i < length) &&(j < length)) 
11         {
12             while((I <length) && (Array [I]% 2 == . 1 ))
 13 is              { // find the first even- 
14                  I ++ ;
 15              }
 16              J = I + . 1 ;
 . 17              the while ((J <length) && (Array [J]% 2 == 0 ))
 18 is              { // find the first odd 
. 19                  J ++ ;
 20 is              }
 21 is              IF ((J == length) || (I == length)) 
 22 is                  BREAK ;
 23 is             Array = tmp [J]; // odd cached 
24              for ( int K = J; K> I; K-- )
 25                  Array [K] = Array [- K- . 1 ];
 26 is              Array [I] = tmp; // odd into its position should be 
27          }
 28          }
 29      }
 30 };

The code above, i and j to represent a double index. In the above code, I i and j times the length of the array index value and the length is compared, the purpose is to prevent cross-border.

For example: the beginning if (length> 1), is to illustrate that there are at least two array elements. Find the following first odd and one even, we also add restrictions i <length and j <length of. In order to avoid cross-border, such as: where the full array is added or all odd and even-numbered, will inevitably lead to cross-border.

to sum up:

I was thinking of painting the core code is not a lot of time, but the problem of preventing the array bounds toss for a long time, they finally submit up, I want to show the point of view is:

  • Where an array of problems is bound to consider cross-border issues, in a timely manner boundary constraints.
  • Where an array of problems, we must consider the array is empty, there is an element of the array, the array has n elements of the case
  • Where an array of problems, almost double the index has become a must-solving ideas.

Make a note of all of the three! ! !

Guess you like

Origin www.cnblogs.com/shaonianpi/p/12357206.html