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. CodeForces 540A Combination Lock (水题)

    题意:给定一个串数,表示一种密码锁,再给定一串密码,问你滑动最少的次数,把第一行变成第二行. 析:很简单么,反正只有0-9这个10个数字,那么就是把每一个数从正着滑和倒着滑中找出一个最小的即可,正着滑 ...

  2. 如何使用office2016发布CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  3. Hadoop-2.4.0分布式安装手册

    目录 目录 1 1. 前言 2 2. 部署 2 2.1. 机器列表 2 2.2. 主机名 2 2.2.1. 临时修改主机名 3 2.2.2. 永久修改主机名 3 2.3. 免密码登录范围 4 3. 约 ...

  4. jmeter监控内存,CPU等方法

    方法1: 使用插件来监控CPU,内存等的使用情况 1.需要的插件准备 JMeterPlugins-Standard-1.4.0.zip , JMeterPlugins-Extras-1.4.0.zip ...

  5. freePCRF免费版体验

    [摘要]遍寻网络数昼夜,未得开源PCRF,亦未得有参考价值的PCRF相关文档.所幸觅得免费体验版freePCRF软件.可窥见PCRF设计思路.方法:PCC规则定义.管理策略:遂记录安装.体验心得. f ...

  6. CodeForces 122G Lucky Array(一脸懵逼的树状数组)

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...

  7. Linq实战 之 Linq to Sql及Entity Framework操作详解

    Linq实战 之 Linq to Sql及Entity Framework操作详解 一:linq to db的框架 1. linq to sql 2. linq to ado.net entity f ...

  8. 等等,你可能误解nodejs了–通俗的概括nodejs的真相

    最近刚把产品从cpp平台迁移到nodejs平台了.  很多以前关于nodejs的观念被颠覆了. 这里分享出来, 欢迎大家批评指正. "nodejs是做服务器端开发的, 它一定和web相关,几 ...

  9. scvmm2008 错误 2912 0x80041001

    执行scvmm系列作业时抛出错误 2912 0x80041001. 这个原因是由于主机和vmm通信媒介bits服务挂起所引起的,bits全称Background Intelligent Transfe ...

  10. asp.net—— 基础之截取字符串

    在实际开发中有时难免会遇到需要获取某个字符串中的某些字符串,这里我们可以用到字符串截取的办法. 截取字符串的方法很容易(暂不包含中文字符串),只要稍微有点.net基础的人看了都能看懂. /// < ...