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 ...
随机推荐
- 代码查重工具sim
在瞎搜东西的时候,发现了一个大牛的博客 看起来很厉害的样子...做了一个LaTeX的语法检查并给出适当的提示,上wiki上一查发现他竟然是CVS第一个版本的发明者和开发者...Dick grune这是 ...
- java 队列的使用(转载)
转载声明:http://blog.csdn.net/lzy_lizhiyang/article/details/48311925 先我们要知道使用队列的目的是什么?一般情况下,如果是一些及时消息的处理 ...
- java 重写父类构造器
- OSPF虚连接简单配置
实验实例:<华为路由器学习指南>P715 本实例的拓扑结构如图:Area 2没有直接与骨干区域直接相连,Area 1被用作传输区域来连接Area 0与Area2.为了使Area2与骨干区域 ...
- Django模板语言循环字典
1. 对于字典,可以有下列用法: {% for row in user_dict.keys %} {% for row in user_dict.values %} {% for row in use ...
- QT 主窗口和子窗口相互切换示例
QT 主窗口和子窗口相互切换示例 文件列表: SubWidget.h #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QtWidgets/QW ...
- 基于注解的spring mvc 中使用 ajax json 的model
在 Spring mvc3中,响应.接受 JSON都十分方便. 使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON. 使用 @RequestBod ...
- 【JavaScript】面向对象的程序设计
一.前言 接着上一篇的内容,继续JavaScript的学习. 二.内容 属性类型 //数据属性[Configurable] —— 能否通过delete删除属性从而重新定义属性,能否修改属 ...
- 【BZOJ2876】【NOI2012】骑行川藏(数学,二分答案)
[BZOJ2876][NOI2012]骑行川藏(数学,二分答案) 题面 BZOJ 题解 我们有一个很有趣的思路. 首先我们给每条边随意的赋一个初值. 当然了,这个初值不会比这条边的风速小. 那么,我们 ...
- LOJ2430:[POI2014]沙拉餐厅Salad Bar——题解
https://loj.ac/problem/2430 是的我BZOJ又没卡过……懒得卡了. 参考:https://blog.csdn.net/zqh_wz/article/details/52887 ...