您的位置:首页 > 其它

lintcode-easy-Partition Array by Odd and Even

2016-03-03 08:12 567 查看
Partition an integers array into odd number first and even number second.

Given
[1, 2, 3, 4]
, return
[1, 3, 2, 4]


public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public void partitionArray(int[] nums) {
// write your code here;

if(nums == null || nums.length <= 1)
return;

int left = 0;
int right = nums.length - 1;

while(true){
while(left < right && nums[left] % 2 != 0)
left++;
while(left < right && nums[right] % 2 == 0)
right--;

if(left == right)
break;
swap(nums, left, right);
}

return;
}

public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;

return;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: