题目:

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

样例

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

挑战

在原数组中完成,不使用额外空间。

解题:

一次快速排序就可以得到结果

Java程序:

  1. public class Solution {
  2. /**
  3. * @param nums: an array of integers
  4. * @return: nothing
  5. */
  6. public void partitionArray(int[] nums) {
  7. // write your code here;
  8. int left = 0;
  9. int right = nums.length - 1;
  10. quick(nums,left,right);
  11. }
  12. public void quick(int[] nums,int left,int right){
  13. int i=left;
  14. int j=right;
  15. if(i>=j)
  16. return;
  17. while(i<j){
  18. int tmp = nums[i];
  19. while(i<j && nums[j]%2==0) j--;
  20. if(i<j){
  21. nums[i++] = nums[j];
  22. }
  23. while(i<j &&nums[i]%2==1) i++;
  24. if(i<j){
  25. nums[j--] = nums[i];
  26. }
  27. nums[i] = tmp;
  28. }
  29. }
  30. }

Python程序:

  1. class Solution:
  2. # @param nums: a list of integers
  3. # @return: nothing
  4. def partitionArray(self, nums):
  5. # write your code here
  6. left = 0
  7. right = len(nums) - 1
  8. while left<right:
  9. tmp = nums[left]
  10. while left<right and nums[right]%2==0:
  11. right-=1
  12. if left<right:
  13. nums[left] = nums[right]
  14. left +=1
  15. while left<right and nums[left]%2==1:
  16. left+=1
  17. if left<right:
  18. nums[right] = nums[left]
  19. right-=1
  20. nums[left] = tmp

总耗时: 408 ms

lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组的更多相关文章

  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. Lintcode373 Partition Array by Odd and Even solution 题解

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

  3. Partition Array by Odd and Even

    Partition an integers array into odd number first and even number second. Example Given [, , , ], , ...

  4. LintCode "Partition Array by Odd and Even"

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

  5. [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

  6. LintCode之奇偶分割数组

    题目描述: 我的分析:题目要求将奇数放在偶数的前面,没有要求将奇数或偶数排序,因此我可以设置两个指针,一个(i)指向数组第一个数字,另一个(j)指向数组的最后一个数字,因为奇数要放在前面,所以从后往前 ...

  7. Leetcode922.Sort Array By Parity II按奇偶排序数组2

    给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...

  8. LintCode 373: Partition Array

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

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

随机推荐

  1. js设计模式(7)---装饰者模式

    0.前言 下午做事效率很低,无精打采的,整个脑子就跟浆糊一样,看看时间一点点流去,心中只能无可奈何,哎,码农的激情难道就这么容易熄灭吗? 1.该模式的使用情况 假如我们想给对象增加功能,但是又不想修改 ...

  2. WIN10主动推升级,有点意思

    不论正与盗,皆推升级,是否一样可用?

  3. 基于php下载文件的详解

    本篇文章是对php下载文件进行了详细的分析介绍,需要的朋友参考下 php下载文件,比如txt文件. 出现的效果就是,弹出浏览器自带的下载框,出现另存为操作.有时候会出现内存溢出和超时的现象. 超时的话 ...

  4. WordPress 主题开发 - (三) 开发工具 待翻译

    Before we get started building any WordPress Theme, we’re going to need to get our development tools ...

  5. 重拾C,一天一点点

    数据类型及长度 char        字符型,占用一个字节 int          整型,通常代表特定机器中整数的自然长度 short       16位 int         16位或32位 ...

  6. linux安装IPython四种方法

    IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性.特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键,IPytho ...

  7. php strpos 用法实例教程

    定义和用法该strpos ( )函数返回的立场,首次出现了一系列内部其他字串. 如果字符串是没有发现,此功能返回FALSE . 语法 strpos(string,find,start) Paramet ...

  8. XAML 概述二

    通过上一节我们已经对XAML有了一定的了解,这一节我们来系统的学习一下XAML. 一. 简单属性与类型转换器,属性元素: 我们已经知道 XAML是一种声明性的语言,并且XAML解析器会为每个标签创建一 ...

  9. JAVASCRIPT、ANDROID、C#分别实现普通日期转换多少小时前、多少分钟前、多少秒

    貌似最近很流行这个,就写了个js函数实现之 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...

  10. Careercup - Facebook面试题 - 23869663

    2014-05-02 03:37 题目链接 原题: A string is called sstring if it consists of lowercase english letters and ...