LintCode_373_奇偶分割数组

来源:互联网 发布:tuxera ntfs for mac 编辑:程序博客网 时间:2024/06/10 09:52

分割一个整数数组,使得奇数在前偶数在后。

样例

给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]

挑战 

在原数组中完成,不使用额外空间。

把偶数放后面就好了吧

public class Solution {    /**     * @param nums: an array of integers     * @return: nothing     */    public void partitionArray(int[] nums) {        // write your code here;        int max = nums.length - 1;        int i = 0;        while(i < max){            if(nums[i] % 2 == 0){                int tmp = nums[i];                nums[i] = nums[max];                nums[max] = tmp;                max--;            }else{                i++;            }        }    }}


0 0
原创粉丝点击