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. position与offset的区别

    .offset()是相对于文档(document)的当前位置,.position()是相对于父级元素的位移,一个元素可以嵌套多个position

  2. SQL日期跟时间值序列

    与数据操作相关的场景要生成日期和时间序列,序列的范围是从输入值@start到@end,且具有一定的时间间隔.这样的场景包括填充数据仓库中的时间维度.应用程序的运行时间安排以及其他.可以借助http:/ ...

  3. [GO]定时器的停止和重置

    package main import ( "time" "fmt" ) func main() { timer := time.NewTimer( * tim ...

  4. libevent学习

    libevent是一个开源的事件控制机制,如果不想陷入多进程或多线程的困扰,那么libevent将是很合适的工具. libevent提供了很多的API来管理和控制事件,可用于设计读.写.信号.定时等各 ...

  5. C/C++语言中的函数参数传参三种对比

    学了很长时间C/C++有时指针方面还是有点乱. 希望大神发现如果下面有不对的地方请指出.我发现之所以我乱就是因为中文表述不准确的问题,比如 ,地址值和地址 #include <iostream& ...

  6. TCP三次握手与防火墙规则

    一个(tct)socket连接需要在客户端与服务端开启一个隧道,客户端提供一个端口(new时可指定,也可不指定,随机),服务端的端口和地址一定要指定.在win下,服务端创建监听端口时,防火墙会提示阻止 ...

  7. 在 LINQ to Entities 查询中无法构造实体或复杂类型“Mvc_MusicShop_diy.Models.Order”

      错误代码: var orders = db.Orders.Where(o => o.UserId == userid).Select(c => new Order {   OrderI ...

  8. [Erlang03]Erlang有哪些好用的静态分析工具?

    1. dialyzer Dialyzer starts its analysis from either debug-compiled BEAM bytecode or from Erlang sou ...

  9. 使用bmfont制作字体

    本地显示正常 将制作好的字体上传 别人用不好使 制作完场景没ctrl+s 保存 ctrl+s保存之后生成另外的文件

  10. 【QTP小技巧】02_QTP中Complete Word 实现(转载)

    相信做过开发或者写过JAVA的朋友都应该对Complete Word这个词语不陌生吧~ 对~~~它就是开发脚本过程中的催化剂 有了它我们就可以不用去死记硬背 有了它我们就不会出现某个代码少一个字母 有 ...