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 ...
随机推荐
- js设计模式(7)---装饰者模式
0.前言 下午做事效率很低,无精打采的,整个脑子就跟浆糊一样,看看时间一点点流去,心中只能无可奈何,哎,码农的激情难道就这么容易熄灭吗? 1.该模式的使用情况 假如我们想给对象增加功能,但是又不想修改 ...
- WIN10主动推升级,有点意思
不论正与盗,皆推升级,是否一样可用?
- 基于php下载文件的详解
本篇文章是对php下载文件进行了详细的分析介绍,需要的朋友参考下 php下载文件,比如txt文件. 出现的效果就是,弹出浏览器自带的下载框,出现另存为操作.有时候会出现内存溢出和超时的现象. 超时的话 ...
- WordPress 主题开发 - (三) 开发工具 待翻译
Before we get started building any WordPress Theme, we’re going to need to get our development tools ...
- 重拾C,一天一点点
数据类型及长度 char 字符型,占用一个字节 int 整型,通常代表特定机器中整数的自然长度 short 16位 int 16位或32位 ...
- linux安装IPython四种方法
IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性.特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键,IPytho ...
- php strpos 用法实例教程
定义和用法该strpos ( )函数返回的立场,首次出现了一系列内部其他字串. 如果字符串是没有发现,此功能返回FALSE . 语法 strpos(string,find,start) Paramet ...
- XAML 概述二
通过上一节我们已经对XAML有了一定的了解,这一节我们来系统的学习一下XAML. 一. 简单属性与类型转换器,属性元素: 我们已经知道 XAML是一种声明性的语言,并且XAML解析器会为每个标签创建一 ...
- JAVASCRIPT、ANDROID、C#分别实现普通日期转换多少小时前、多少分钟前、多少秒
貌似最近很流行这个,就写了个js函数实现之 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
- Careercup - Facebook面试题 - 23869663
2014-05-02 03:37 题目链接 原题: A string is called sstring if it consists of lowercase english letters and ...