[LintCode]Partition Array by Odd and Even
public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public void partitionArray(int[] nums) {
// 2016-04-08
// 题目解读:输入输出皆可无序
if (nums == null && nums.length == 0) {
return;
}
int oddPtr = 0;
int evenPtr = nums.length - 1;
while (true) {
// 从左向右找even
while (oddPtr < evenPtr && nums[oddPtr] % 2 == 1) {
// if odd
oddPtr++;
}
// 从右向左找odd
while (oddPtr < evenPtr && nums[evenPtr] % 2 == 0) {
// if even
evenPtr--;
}
// swap
if (oddPtr < evenPtr) {
int temp = nums[oddPtr];
nums[oddPtr] = nums[evenPtr];
nums[evenPtr] = temp;
} else {
return;
}
}
}
}