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 = , io = , ie = len - ;
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"的更多相关文章

  1. 373. Partition Array by Odd and Even【LintCode java】

    Description Partition an integers array into odd number first and even number second. Example Given  ...

  2. Lintcode373 Partition Array by Odd and Even solution 题解

    [题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...

  3. Partition Array by Odd and Even

    Partition an integers array into odd number first and even number second. Example Given [, , , ], , ...

  4. lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组

    题目: 奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 解题: 一次快速排序就可 ...

  5. Lintcode: Partition Array

    Given an array "nums" of integers and an int "k", Partition the array (i.e move ...

  6. LintCode 373: Partition Array

    LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...

  7. A. Array with Odd Sum Round #617(水题)

    A. Array with Odd Sum time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  9. Partition Array

    Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...

随机推荐

  1. OpenvSwitch架构

    Openvswitch的架构 数据库结构和OVS-VSCTL # ps aux | grep openvswitch root      1117  0.0  0.0  21200  1580 ?   ...

  2. C# 读取文本文档(转)

    1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出. byte[] byData = ...

  3. (基础篇)PHP与Web页面交互

    PHP与Web页面交互是实现PHP网站与用户交互的重要手段.在PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递. 这里我们将详细讲解表单的相关知识,为 ...

  4. MACOS,LINUX,IOS上可用的毫秒级精度时间获取

    二话不说,先上代码 void outputCurrentTime(uint32_t seq,const char* type){ struct timeval t_curr; gettimeofday ...

  5. 启动tomcat不出现命令窗口

    有个软件要安装在U盘中,B/S结构,用tomcat做应用服务器,客户要求tomcat不能注册为系统服务,启动时tomcat启动时不能出现命令行窗口,怎么实现? 根据你的问题描述,猜测你的部署系统是Wi ...

  6. html---文本框样式;

    一.一个单行文本框的例子 <form name="form1" action="mailto:3400982550@qq.com" method=&quo ...

  7. leetcode 128. Longest Consecutive Sequence ----- java

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. 继承:《原型和原型链(prototype 属性使您有能力向对象添加属性和方法。)》

    二. 原型对象   在JavaScript 中,每当定义一个对象(函数)时候,对象中都会包含一些预定义的属性.其中函数对象的一个属性就是原型对象 prototype.注:普通对象没有prototype ...

  9. STM32的Cortex-M3核与ARM7有何区别?哪个性能更强?

  10. unbuntu下安装flash插件

    adobe flash player的官方下载页面为:https://get.adobe.com/cn/flashplayer/ 不过近期通过APT方式以及ubuntu的软件中心都安装不了flashp ...