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 ...
随机推荐
- Java 线程安全问题
线程安全问题产生原因: 1.多个线程操作共享的数据: 2.操作共享数据的线程代码有多条. 当一个线程正在执行操作共享数据的多条代码过程中,其它线程也参与了运算, 就会导致线程安全问题的发生. cl ...
- String、Date、Calendar之间的转换
1.String.Date.Calendar之间的转换 要用到格式化类SimpleDateFormat package com.rong.se; import java.text.ParseExcep ...
- selenium webdriver 表格的定位方法练习
selenium webdriver 表格的定位方法 html 数据准备 <html> <body> <div id="div1"> <i ...
- Laravel 框架集成 UEditor 编辑器的方法
㈠. 背景 在项目开发的过程中,免不了使用修改功能,而富文本编辑器是极为方便的一种推荐,当然,个人认为 MarkDown 更为简单,但是感觉暂时只适合程序猿 此文介绍如何在 Laravel5.5 ...
- 博客搬家 --- CSDN
博客园好久没维护了,搬家吧 欢迎移步到新博客
- HDU4802_GPA
水题. #include <iostream> #include <cstdio> #include <cstring> using namespace std; ...
- (转)Spring用代码来读取properties文件
转至http://www.cnblogs.com/Gyoung/p/5507063.html 我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org ...
- DataTable 转换 DataSet
DataTable dt = resuylt.Copy(); var dsR = new DataSet(); ds.Tables.Add(dt);
- 【CF700E】Cool Slogans(后缀自动机)
[CF700E]Cool Slogans(后缀自动机) 题面 洛谷 CodeForces 题解 构建后缀自动机,求出后缀树 现在有个比较明显的\(dp\) 设\(f[i]\)表示从上而下到达当前点能够 ...
- 洛谷 P4240 毒瘤之神的考验 解题报告
P4240 毒瘤之神的考验 题目背景 \(\tt{Salamander}\)的家门口是一条长长的公路. 又是一年春天将至,\(\tt{Salamander}\)发现路边长出了一排毒瘤! \(\tt{S ...