题目:奇偶分割数组

来源:互联网 发布:淘宝如何申请海外买手 编辑:程序博客网 时间:2024/05/15 11:40

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

您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?AirbnbAlibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits
感谢您的反馈
样例

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

挑战

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

标签 Expand
两根指针数组



相关题目 Expand         

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



0 0