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

Example
Given [, , , ], return [, , , ] Challenge
Do it in-place.

将数组中的奇数和偶数分开,使用『两根指针』的方法最为自然,奇数在前,偶数在后,若不然则交换之。

JAVA:

public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public void partitionArray(int[] nums) {
if (nums == null) return; int left = 0, right = nums.length - 1;
while (left < right) {
// odd number
while (left < right && nums[left] % 2 != 0) {
left++;
}
// even number
while (left < right && nums[right] % 2 == 0) {
right--;
}
// swap
if (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
}
}

源码分析

注意处理好边界即循环时保证left < right.

复杂度分析

遍历一次数组,时间复杂度为 O(n), 使用了两根指针,空间复杂度 O(1).

Partition Array by Odd and Even的更多相关文章

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

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

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

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

  3. LintCode "Partition Array by Odd and Even"

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

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

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

  5. LintCode 373: Partition Array

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

  6. 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 ...

  7. Lintcode: Partition Array

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

  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. Ubuntu14.04下redis安装 配置, redis主从配置

    1.到官网下载redis源码包 wget http://download.redis.io/releases/redis-3.2.8.tar.gz 2.解压 并 编译 .tar.gz cd redis ...

  2. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoader

    一直报这个错误 错误原因jar包没有导入到.classes文件下,需要导入到此文件下,因为用的是user library,所以只有逻辑导入,没有实际导入,切换下就好了,具体看如下文章 http://w ...

  3. CodeForces 342B Xenia and Spies (水题模拟,贪心)

    题意:给定 n 个间谍,m个区间,一个 s,一个f,然后从 s开始传纸条,然后传到 f,然后在每个 t 时间在区间内的不能传,问你最少的时间传过去. 析:这个题,就模拟一下就好,贪心策略,能传就传,找 ...

  4. CodeForces 540B School Marks (贪心)

    题意:先给定5个数,n,  k, p, x, y.分别表示 一共有 n 个成绩,并且已经给定了 k 个,每门成绩 大于0 小于等于p,成绩总和小于等于x, 但中位数大于等于y.让你找出另外的n-k个成 ...

  5. python-字符串-技巧

    1.删除字符串末尾空白:rstrip函数 test1 = "This is a test " print(test1.rstrip()) 但是这种删除只是暂时的,如果想永久删除,则 ...

  6. 关于HTTP协议传输与接收数据的相关内容

    第一篇: HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/28/2612910.html 第二篇: 深入浅出U ...

  7. ArcGis Android 10.2.6更新文档翻译

    ArcGis Android 10.2.6更新文档翻译 @[ArcGis Android|10.2.6|更新文档] 本文描述了ArcGIS Runtime SDK for Android 10.2.6 ...

  8. How to: Cancel a Task and Its Children

    http://msdn.microsoft.com/en-us/library/dd537607.aspx using System; using System.Collections.Concurr ...

  9. C#串口数据互通小程序

    主要功能: 所编写的程序需将串口1.串口2数据互通,即:串口1接收到数据的同时将数据通过串口2发出,串口2接收到数据的同时将数据通过串口1发出. 并根据需要由指定串口发送或获取数据. 代码如下: us ...

  10. WebStrom创建自定义后缀名的文件

    如下图所示,我在下面的项目里面创建了.ejs文件,但是正常的情况下我们WebStrom是无法创建.ejs文件的,那么我们怎么才能让我们的WebStrom拥有创建该后缀名的文件的能力呢? 不BB,直接看 ...