LintCode 373. Parity Split Array

topic:

  • LintCode 373. Parity Split Array
  • Divide an array of integers so that odd numbers come first and even numbers last.

Example:

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

refer to

accomplish:

  • Java实现代码

    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==0)  return;
            int i = 0;
            int j = nums.length - 1;
            while(i<j){
                while(nums[i]%2==1){
                    i++;
                }
                while(nums[j]%2==0){
                    j--;
                }
                if(i<j){
                    nums[i] = nums[i]^nums[j];
                    nums[j] = nums[i]^nums[j];
                    nums[i] = nums[i]^nums[j];
                }
            }
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325195893&siteId=291194637