lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组
题目:
分割一个整数数组,使得奇数在前偶数在后。
给定 [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 奇偶分割数组的更多相关文章
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- Lintcode373 Partition Array by Odd and Even solution 题解
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...
- Partition Array by Odd and Even
Partition an integers array into odd number first and even number second. Example Given [, , , ], , ...
- LintCode "Partition Array by Odd and Even"
One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...
- [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 ...
- LintCode之奇偶分割数组
题目描述: 我的分析:题目要求将奇数放在偶数的前面,没有要求将奇数或偶数排序,因此我可以设置两个指针,一个(i)指向数组第一个数字,另一个(j)指向数组的最后一个数字,因为奇数要放在前面,所以从后往前 ...
- Leetcode922.Sort Array By Parity II按奇偶排序数组2
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...
- LintCode 373: Partition Array
LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...
- 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 ...
随机推荐
- HTML5应用开发:JavaScript库iScroll教程
目录 1. iScroll介绍 2. 安装和使用 3. 简单的iScroll例子 4. Pinch & Zoom 5. Snap to element 6. iScroll 详细参数 1. i ...
- 【Qt】Qt之启动外部程序【转】
简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...
- 为 WordPress 标签添加 rel="nofollow" 属性
WordPress 标签默认并无 rel="nofollow" 属性.rel="nofollow" 属性的作用是:告诉搜索引擎,无需追踪目标页,禁止蜘蛛爬行和传 ...
- ASP.NET中的TextBox下划线
看到园子里一位同行写的 http://www.cnblogs.com/xiaopeng84/archive/2007/04/10/707093.html 但是没有贴出效果图,自己练了一下,贴出代码和效 ...
- allegro生成光绘文件时,通过cam打开,*.drl钻孔文件不识别,为Unknow类型
生成钻孔文件时,NC_Parameters中,应该选Absolute
- verilog逻辑复制
本文转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/27/FPGA_verilog.html 在FPGA设计中经常使用到逻辑复制,逻辑复制 ...
- 【BZOJ 1068】[SCOI2007]压缩
Description 给 一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小写字母外还可以(但不必)包含大写字母R与M,其中M 标记重复串的开始,R重复从 ...
- C# ASP .NET WEB方向和WPF方向,我该如何去选择
一个2012年南航毕业学软件的学生,该如何去选择我的职业方向? 2011年11分月份,我被老师介绍在南京珠江路华丽国际大厦工作,开发一个大型国际物流平台,公司的开发人员比较少,设计网站的是高校的老师, ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- linux使用:vi编辑器
初学linux,目前是概念多于操作,所以记录下一些操作: 编辑某个文件():vi 文件名 编辑后保存退出::wq 编辑后不保存退出: :q! 参数:-R 只读模式 -x 文件加密(vim命令下使用) ...