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. 找"数学口袋精灵"bug

    团队成员的博客园地址: 刘森松:http://home.cnblogs.com/u/lssh/ 郭志豪:http://home.cnblogs.com/u/gzh13692021053/ 谭宇森:ht ...

  2. 2nd 阅读构建之法有感

    阅读构建之法有感 利用这一周的时间,我大致了解构建之法一书,这本书带我走进了一个全新的领域.它让我以一种新的视角去了解软件产业的发展和工作,领略软件工程的独特魅力,更给出了简单易懂的方式去理解何为软件 ...

  3. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...

  4. sqlserver-触发器-判断更新了哪个字段。

    create trigger 触发器名称on 表名(将触发器创建到那张表中)for updateasif update(判断更新字段)beginupdate (要更新的表名) set 字段=inser ...

  5. 内存测试——Android Studio中对应进程的Heap

    通过Android Studio的Heap查看该程序的目前占用内存大小,多次进出界面,观察内存内存大小的变化.用Heap监测应用进程使用内存情况的步骤如下: 1. 启动Android Studio—& ...

  6. java学习 猜数字

    package study; import java.util.Scanner; /** * 猜数字小游戏 * * @author carry * */ public class GuessNumbe ...

  7. 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中

    一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字. 二.利用代码给ListView添加 ...

  8. BZOJ 1066:[SCOI2007]蜥蜴(最大流)

    蜥蜴Description在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...

  9. 【明哥报错簿】之【HTTP Status 500 - Servlet.init() for servlet mvc-dispatcher threw exception】

    报错:java.lang.NoClassDefFoundError: /factory/config/EmbeddedValueResolver spring或者jdk的问题,解决办法:spring3 ...

  10. [十七]SpringBoot 之 使用自定义的properties

    Springboot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 如果继续在application.properties中添加 如: ...