lintcode-easy-Partition Array by Odd and Even

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Partition Array by Odd and Even相关的知识,希望对你有一定的参考价值。

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;
    }
    
}

 

以上是关于lintcode-easy-Partition Array by Odd and Even的主要内容,如果未能解决你的问题,请参考以下文章

20170924 a20170924 a

a++和++a的区别

逻辑代数

python运算符

运算符

arraylist排序 例如值 a104,a106,a102,a92,a98,a94 结果a92,a94,a98,a102,a104,a106