## Monday, January 11, 2016

### Partition Array by Odd and Even | 刷题

Partition Array by Odd and Even | 刷题
Partition an integers array into odd number first and even number second. Example Given [1, 2, 3, 4], return [1, 3, 2, 4] Challenge Do it in-place.
``````    public void partitionArray(int[] nums) {
int start=0;
int cur=0;
int end=nums.length-1;

while(cur<end){
if(nums[cur]%2!=0){
swap(nums, start++, cur++);
}
else{
swap(nums, cur, end--);
}
}
}``````
http://cherylintcode.blogspot.com/2015/06/partition-array-by-odd-and-even.html
public void partitionArray(int[] nums) {
if(nums == null || nums.length == 0) return;

int left = 0, right = nums.length - 1;
while(left < right){
while(left < right && nums[left] % 2 == 1) left++;
while(left < right && nums[right] % 2 == 0) right--;
if(left < right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right--;
}

}
}
https://codesolutiony.wordpress.com/2015/05/25/lintcode-partition-array-by-odd-and-even/
`    ``public void partitionArray(int[] nums) {`
`        ``if (nums == null || nums.length <= 1) {`
`            ``return;`
`        ``}`
`        ``int even = nums.length;`
`        ``for (int i = 0; i < even; i++) {`
`            ``if (nums[i] % 2 == 0) {`
`                ``int tmp = nums[i];`
`                ``nums[i] = nums[--even];`
`                ``nums[even] = tmp;`
`                ``i--;`
`            ``}`
`        ``}`
`    ``}`
http://www.chenguanghe.com/partition-array-by-odd-and-even/

public void partitionArray(int[] nums) {
if(nums.length == 0 || nums == null)
return;
int i = 0;
int j = nums.length - 1;
while(i < j) {
while(i<j && nums[i] % 2 != 0) // 如果左指针的位置是奇数
i++;
while(i< j && nums[j] % 2 == 0) // 如果右指针的位置是偶数
j--;
if(i < j)
swap(nums,i++,j--);
}
}