首页 > 其他 > 详细

LintCode "Partition Array by Odd and Even"

时间:2015-08-29 15:19:27      阅读:334      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!