题目:

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

样例

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

挑战

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

解题:

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

Java程序:

public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public void partitionArray(int[] nums) {
// write your code here;
int left = 0;
int right = nums.length - 1;
quick(nums,left,right);
}
public void quick(int[] nums,int left,int right){
int i=left;
int j=right;
if(i>=j)
return;
while(i<j){
int tmp = nums[i];
while(i<j && nums[j]%2==0) j--;
if(i<j){
nums[i++] = nums[j];
}
while(i<j &&nums[i]%2==1) i++;
if(i<j){
nums[j--] = nums[i];
}
nums[i] = tmp;
}
}
}

Python程序:

class Solution:
# @param nums: a list of integers
# @return: nothing
def partitionArray(self, nums):
# write your code here
left = 0
right = len(nums) - 1
while left<right:
tmp = nums[left]
while left<right and nums[right]%2==0:
right-=1
if left<right:
nums[left] = nums[right]
left +=1
while left<right and nums[left]%2==1:
left+=1
if left<right:
nums[right] = nums[left]
right-=1
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. 【Qt】Qt实战一二三【转】

    简介 “我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流”,不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的状态,来开始每一天的 ...

  2. Gulp vs Grunt 前端构建工具对比

    Gulp vs Grunt 前端工程的构建工具对比 1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Jav ...

  3. Redis源码研究--跳表

    -------------6月29日-------------------- 简单看了下跳表这一数据结构,理解起来很真实,效率可以和红黑树相比.我就喜欢这样的. typedef struct zski ...

  4. hexo-github 博客搭建

    安装nodejs 从官网下载系统对应的源码 wget -qO- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | ...

  5. Linq中延迟查询和立即查询

    //立即查询 public static void NowExecute() { var results = new int[]{5,4,3,2,1,6,7,8,9,0 }; int i = 0; v ...

  6. hadoop自动安装的脚本与步骤

    最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...

  7. Java从入门到精通——技巧篇之利用dom4j取出XML文件中的数据

    在我们做项目的时候会经常用到XML文件用来配置系统,XML让系统更加的具有了灵活性,Java如何从XML中取出我们想要的数据呢?下面是我利用DOM4J来实现取出XML文件中的数据. XML文件 < ...

  8. 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)

    一般在使用视图进行查询时,视图中意般都关联了两个或者更多个表,一般在这种情况下才会使用视图,但是但我在使用视图来查询数据时没有问题,但是一旦在分页中使用到视图进行查询就会出现错误提示如下: ADODB ...

  9. CentOS中操作Psql

    psql -h 172.16.35.179 -U username -d dbname sername为数据库用户名,dbname为要连接的数据库名 查看现有的数据库:  \l或\list 查看所有列 ...

  10. iOS 进阶 第九天(0408)

    0408 makekeyAndVisible解释 一个程序可以有多个Window,但只有一个窗口能够成为主窗口.如图中所示,此时的window2是主窗口.主窗口用处大了.从iOS7开始无论是主窗口还是 ...