LintCode 373: Partition Array

题目描述

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

样例

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

Thu Feb 23 2017

思路

简单题,可以很自然地想到再用一个答案数组,从头到尾遍历一遍,遇到奇数就放到答案数组的前面,遇到偶数就放到答案数组的后面。

还有另一种方法,跟快速排序的形式有点像,即从前面找到一个偶数,同时从后面找到一个奇数,将两个数调换。

虽然两种方法的时间复杂度都是\(O(n)\),但是第二种方法的空间复杂度是\(O(1)\),算是更优的方法。

代码

// 奇偶分割数组
void partitionArray(vector<int> &nums)
{
if (nums.size() <= 0) return;
vector<int>::iterator l = nums.begin(), r = nums.end() - 1;
while(l != r)
{
while (l != r && *l % 2 == 1) ++l;
while (l != r && *r % 2 == 0) --r;
swap(*l, *r);
}
}

LintCode 373: Partition Array的更多相关文章

  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. Lintcode: Partition Array

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

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

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

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

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

  5. LintCode "Partition Array by Odd and Even"

    One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...

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

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

  7. Partition Array

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

  8. [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  9. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

随机推荐

  1. 前端系列之HTML基础知识概述

    1.什么是HTML HTML:Hyper Text Markup Language :超文本标记语言. 超文本:功能比普通文本更加强大. 标记语言:使用一组标签对内容进行描述的语言,它不是编程语言. ...

  2. Mac10.11.2 Apache 服务配置

    系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...

  3. javascript之容易出错的地方

    1: 不是所有的非空对象都有toString()方法的 var obj = Object.create(null); console.log(obj.toString());   // false; ...

  4. PHP之implode()方法

    implode — 将一个一维数组的值转化为字符串 string implode ( string $glue , array $pieces ) string implode ( array $pi ...

  5. set集合,深浅拷贝以及部分知识点补充

    目录: 1.基础数据类型补充 2.set集合 3.深浅拷贝 一,基础数据类型补充 字符串的基本操作 li = ["李李嘉诚", "麻花藤", "⻩黄海 ...

  6. MYSQL-update与select结合使用

    使用 inner join   ) c ,," , iteration; 如上例子: 完成更新 picture.labels 字段 & picture.iteration自增 的两个 ...

  7. inline函数的总结

    在函数返回类型前加上关键字inline就可以将函数指定为内联函数: inline const string& shortString(const string &s1, const s ...

  8. 【bzoj2669】[cqoi2012]局部极小值 容斥原理+状压dp

    题目描述 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所有相邻格子(相邻是指有公共边或公共顶点)都小,我们说这个格子是局部极小值. 给出所有局部极小值的位置,你的任 ...

  9. android面试(5)---SQL数据库

    SQL基础: 1.如何查询table1从20到30条记录: select * from table1 limit 19,11 2.替换id=1,name =deman的记录? replace into ...

  10. 题解 P1628 【合并序列】

    看到这个题,小金羊第一秒的反应就是: 优先队列可解! 看到楼上某同学一个个比较, find()函数是时候现身了! string//类型库 //find具体用法可以自行百度 //这里仅说这里的用法(逃) ...