interleaving Positive and negative Numbers
Original title Link: http://lintcode.com/zh-cn/problem/interleaving-positive-and-negative-numbers/
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Note
You aren't necessary to keep the original order or positive integers or negative integers.
Sample Example
Given [-1,-2, -3, 4, 5, 6], after Re-range, it'll be [-1, 5,-2, 4, -3, 6] or any other legal answer.
challenges
Do it in-place and without extra memory.
Solution 1:
1. First use parition to divide the array into negative numbers on the left and positive for the right.
2. If the negative numbers are more, swap the excess negative numbers with the trailing values. (So the extra number will be placed at the end of the array)
3. Left points to the right-hand side of the array.
4. The 3rd step, depending on whether it is a positive number, or a negative number, the starting position to change a bit. More positive, we want to start with a positive number:
For example 3-1 2
Negative number, we want to start with negative numbers, such as-1 3-2
5. Constantly swap left, right hands, and advance a long time before 2. Until left and right meets.
1 classSolution {2 /**3 * @parama:an an integer array.4 * @returnAn integer array5 */6 //Solution 2: Judging a positive number or a negative number. 7 Public Static int[] Rerange (int[] A) {8 //Write your code here9 Ten //Check the input parameter. One if(A = =NULL|| A.length = = 0) { A returnA; - } - the intLen =a.length; - - intleft =-1; - intright =a.length; + - //divide the negative and positive integers. + while(true) { A while(Left < a.length-1 && A[++left] < 0); at - while(Left < right && A[--right] > 0); - - if(Left >=Right ) { - Break; - } in - swap (A, left, right); to } + - //Left:point to the first positive number. the intNegnum =Left ; * intPosnum = Len-Left ; $ Panax Notoginseng intLes =math.min (Negnum, posnum); - intdif = Math.Abs (Negnum-posnum); the + //if the negative number is more, throw more negative numbers to the back . A if(Negnum >posnum) { the intCNT =dif; + intL =les; - intr = len-1; $ while(CNT > 0) { $ Swap (A, L, R); -l++; -r--; thecnt--; - }Wuyi the //negative number of times, negative numbers in front, conversely, positive numbers in the front -left =-1; Wu //skip to the right value that does not need to be exchanged -right = A.length-dif; About}Else { $ //positive number in front -left =-2; - //skip to the right value that does not need to be exchanged -right = A.length-dif + 1; A } + the /* - -1-2 -5-6 3 4 les = 2; $ 4-2 -5-6 3-1 the */ the //swap the negative and the positive the while(true) { theLeft + = 2; -Right-= 2; in the if(Left >=les) { the Break; About } the swap (A, left, right); the } the + returnA; - } the Bayi Public Static voidSwapint[] A,intN1,intn2) { the intTMP =A[N1]; theA[N1] =A[N2]; -A[N2] =tmp; - } the}
View CodeGITHUB:
Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/array/Rerange.java
Lintcode:interleaving Positive and negative Numbers problem solving report