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 ...
随机推荐
- xml操作
一.LINQ to XML 编程基础 1.LINQ to XML类 System.Xml.Linq命名空间含有19个类,下表列出了它们的名称及其描述: 类 描述 XAttribute 表示一个 XML ...
- h5 web模板
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...
- JS定时器实例解析
在javascritp中,有两个关于定时器的专用函数. 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...
- UnitTest
using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...
- spring-mysqlclient开源了
https://github.com/risedragon/spring-mysqlclient/wiki/spring-mysqlclient-user-guide 开源了一个项目,总结了几年的数据 ...
- andriod Kernel configuration is invalid
error: ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/conf ...
- phpstorm8 设置及license key
phpstorm8 license key Learn Programming ===== LICENSE BEGIN ===== 63758-12042010 00000Ryqh0NCC73lpRm ...
- 从InputStream到String_写成函数
String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...
- VSC 使用Git进行版本控制
Visual Studio Code 使用Git进行版本控制 请确保你安装了最新的VS Code.http://code.visualstudio.com/ 请确保安装了最新版的Git.https:/ ...
- cxf简单实例
CXF是一个基于 Servlet 技术的 SOA 应用开发框架,简单来说,就是WebService的轻量级实现. 1.下载开发包:http://cxf.apache.org/download.html ...