LintCode373 Partition Array by Odd and Even

  1. 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) {
        // write your code here
        int l = 0;
        int r = nums.length - 1;
        while(l <= r){
            while(l <= r && nums[l] % 2 != 0){
                l++;
            }
            while(l <= r && nums[r] % 2 == 0){
                r--;
            }
            if(l <= r){
                int temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/fruit513/article/details/84890433