LintCode 373: Partition Array
LintCode 373: Partition Array
题目描述
分割一个整数数组,使得奇数在前偶数在后。
样例
给定[1, 2, 3, 4]
,返回[1, 3, 2, 4]
。
Thu Feb 23 2017
思路
简单题,可以很自然地想到再用一个答案数组,从头到尾遍历一遍,遇到奇数就放到答案数组的前面,遇到偶数就放到答案数组的后面。
还有另一种方法,跟快速排序的形式有点像,即从前面找到一个偶数,同时从后面找到一个奇数,将两个数调换。
虽然两种方法的时间复杂度都是\(O(n)\),但是第二种方法的空间复杂度是\(O(1)\),算是更优的方法。
代码
// 奇偶分割数组
void partitionArray(vector<int> &nums)
{
if (nums.size() <= 0) return;
vector<int>::iterator l = nums.begin(), r = nums.end() - 1;
while(l != r)
{
while (l != r && *l % 2 == 1) ++l;
while (l != r && *r % 2 == 0) --r;
swap(*l, *r);
}
}
LintCode 373: Partition Array的更多相关文章
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组
题目: 奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 解题: 一次快速排序就可 ...
- LintCode "Partition Array by Odd and Even"
One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...
- Lintcode373 Partition Array by Odd and Even solution 题解
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum
Given an array A of integers, return true if and only if we can partition the array into three non-e ...
随机推荐
- erlang节点互相ping,一个能ping通,另外一个不行。
今天发现一个问题,2个erlang节点,1个主动ping另外一个不通,然后等待另外一个ping过来,2个节点才连通.记录一下. 首先,erlang节点的cookie是一致的.查了文档,cookie一致 ...
- WebService(三)
JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- 【C】多线程编程笔记
1. pthread_create(pthread类型指针变量 ,NULL ,函数 ,函数参数[多个参数用结构体传]) 2. pthread_join(pthread类型指针变量, 返回一般为null ...
- 用Python实现求Fibonacci数列的第n项
1. 背景——Fabonacci数列的介绍(摘自百度百科): 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacc ...
- python3判断字典、列表、元组为空以及字典是否存在某个key的方法
#!/usr/bin/python3 #False,0,'',[],{},()都可以视为假 m1=[] m2={} m3=() m4={"name":1,"age&quo ...
- Windows搭建Log4Net+FileBeat+ELK日志分析系统过程
参考博客:http://udn.yyuap.com/thread-54591-1-1.html ; https://www.cnblogs.com/yanbinliu/p/6208626.html ; ...
- 对ViewModel自定义约束
有时候我们常要对一些属性进行自定义的约束,可以这么做 using ListSys.Entity; using System; using System.Collections; using Syste ...
- Problem D - Non-boring sequences——Contest1004 - National Day Training Contest -- Day3
今天比赛的时候做的一个坑题.深坑啊. 题目意思是给你一个有n个数的数字序列.要你判断对于这个序列是都满足任意一个子序列都至少含有一个只出现一次的数字. 看完题目后没什么思路,一直以为要用线段树,每次删 ...
- SolrPerformanceFactors--官方文档
原文地址:http://wiki.apache.org/solr/SolrPerformanceFactors Contents Schema Design Considerations indexe ...