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. mySQL CRUD操作(数据库的增删改查)

    一.数据库操作 1.创建数据库 create database 数据库名称 2.删除数据库 drop database 数据库名称   二.表操作 1.创建表 create table 表名 (    ...

  2. php中能够获取到某一网站内容的方法

    方法一:file_get_contents 函数 example: <?php $url = "http://www.cnblogs.com"; $contents = fi ...

  3. 关于limit hashlimit资料整理

    这几天正在捣鼓防火墙,用到了hashlimit模块.Google了一圈发现相关的文档无论英文还 是中文都很少, 所以我就把自己的折腾的心得记录下来吧. hashlimit是iptables的一个匹配模 ...

  4. Easy Problem-map和vector的使用

    给出一个包含n个整数的数组,你需要回答若干询问.每次询问包含两个整数k和v,输出从左到右第k个v的下标(数组下标,从左右到右编号1~n). [输入格式] 输入包含多组数据.每组数据第一行为两个整数n和 ...

  5. Phonegap开发的前后台数据交互

    在用Phonegap开发时,需要进行前后台数据交互,在网上找资料,很多东西让人一头雾水,最后借鉴了下面的博客: http://blog.sina.com.cn/s/blog_681929ae01017 ...

  6. 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. sublime text 2 + Dev-C++/MinGW 组合配置更方便快捷的 C/C++ 编译环境(原创)

    首先看一下配置后的效果: 1.直接在底部文本框中显示运行结果(不需要从键盘输入的时候使用): 2.在cmd中运行结果(需要从键盘输入的时候使用): 快捷键说明: 运行: 在底部文本栏显示结果:Ctrl ...

  8. Servlet 中文乱码问题及解决方案剖析

    转自:http://blog.csdn.net/xiazdong/article/details/7217022/ 一.常识了解 1.GBK包含GB2312,即如果通过GB2312编码后可以通过GBK ...

  9. php脚本时 linux命令获取服务器IP

    exec("netstat -anp | grep smtp",$sendList);   //php脚本获取服务器邮件进程数 exec("netstat -anp |g ...

  10. VC线程中操作控件,引起程序卡死的问题。

    [问题还原] 线程中操作控件,具体为控制一个按键的使能,使能后结束线程. 主程序中有一个死循环,等待线程结束. 然后,就没有然后了-- [解决方案] 在主程序死循环中,如果检测到界面消息,优先处理掉.