码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode "Partition Array by Odd and Even"

时间:2015-08-29 15:20:27      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

One pass in-place solution: all swaps.

class Solution {
public:
    /**
    * @param nums: a vector of integers
    * @return: nothing
    */
    void partitionArray(vector<int> &nums) {
        size_t len = nums.size();
        int i = 0, io = 0, ie = len - 1;
        while (io < ie)
        {
            int v = nums[i];
            if (v & 0x1) // odd
            {
                swap(nums[i], nums[io++]);
            }
            else    // even
            {
                swap(nums[i], nums[ie--]);
            }
            i = io;
        }
    }
};

LintCode "Partition Array by Odd and Even"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4769115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!