Sort Array By Parity LT905
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 50000 <= A[i] <= 5000
Idea 1. Borrow the partition idea from quicksort, like Dutch flag problem, assume the array is already sorted as required, what to do with new element?
even | odd | ?
Time complexity: O(n)
Space complexity: O(1)
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
}
public int[] sortArrayByParity(int[] A) {
int evenEnd = -1;
for(int i = 0; i < A.length; ++i) {
if(A[i]%2 == 0) {
++evenEnd;
swap(A, evenEnd, i);
}
}
return A;
}
}
Idea 1.a Two pointers walking toward each other and swap if not in the right place
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
}
public int[] sortArrayByParity(int[] A) {
for(int left = 0, right = A.length-1; left < right; ) {
while(left < right && A[left]%2 == 0) {
++left;
}
while(left < right && A[right]%2 == 1) {
--right;
}
if(left < right) {
swap(A, left, right);
++left;
--right;
}
}
return A;
}
}
slightly more cleaner
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
}
public int[] sortArrayByParity(int[] A) {
for(int left = 0, right = A.length-1; left < right; ) {
if(A[left]%2 == 1 && A[right]%2 == 0) {
swap(A, left, right);
}
if(A[left]%2 == 0) {
++left;
}
if(A[right]%2 == 1) {
--right;
}
}
return A;
}
}
Idea 2. customised comparator to sort
Time complexity: O(nlogn)
Space complexity: O(n)
class Solution {
public int[] sortArrayByParity(int[] A) {
Integer[] B = new Integer[A.length];
for(int i = 0; i < A.length; ++i) {
B[i] = A[i];
}
Comparator<Integer> cmp = (a, b) -> Integer.compare(a%2, b%2);
Arrays.sort(B, cmp);
for(int i = 0; i < A.length; ++i) {
A[i] = B[i];
}
return A;
}
}
Sort Array By Parity LT905的更多相关文章
- Sort Array By Parity II LT922
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- [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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
随机推荐
- 1.5.7、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置单用户模式)
配置单用户模式 在传统的Cloudera Manager部署中,管理每台主机上的Hadoop进程的Cloudera Manager Agent以root用户身份运行.但是,某些环境会限制对root帐户 ...
- MyBatis数据库连接的基本使用
MyBatis部分: 本部分内容只主要体现Mybatis的特点. (1)MyBatis是什么? 开源的持久层框架,MyBatis的底层仍然是JDBC (2)编程步骤 step1 Maven项目 pom ...
- cakePHP的ajax弹出窗
在html里添加一个触发弹出框的按钮 $("#button1").on("click", function() { $("#dialogue" ...
- Avalon Framework
Apache Avalon has closed. Apache Avalon began in 1999 as the Java Apache Server Framework and in lat ...
- Jmeter+Ant+Jenkins实现接口自动化(转载)
转载自 http://www.cnblogs.com/chengtch/p/6145867.html 本文转载于上面的网址,稍作修改,实用性更强. Jmeter是压力测试.接口测试工具,Ant是基于 ...
- 【C++】operator new/new operator/placement new之间的区别
new operator new operator即是c++中的关键字new.比如A* = new A; 中的new就是new operator. 它执行了三个步骤: 1. 分配内存空间 事实上,分配 ...
- PHP使用UTF8编码读取ACCESS的乱码问题解决方案(转)
PHP使用UTF8编码读取ACCESS的乱码问题解决方案 http://it.xwstudy.com/readnews.php?id=627 来源:本站编辑 发布日期:2013-05-27 已有 17 ...
- Dede文章标题长度修改
方法一.首先你要进入dedecms后台,系统——系统基本参数——其他选项——文档标题最大长度——在这修改为200或更大(其实200应该是足够了). 方法二.进入phpmyadmin,点击dede_ar ...
- OpenVPN 2.2.1 之后期维护
一.Openvpn 用户注销 每个公司都会用员工离职,因此注销vpn用户也就成了运维人员日常工作的一部分. 其实Openvpn在设计的时候也想到了这点,我们可以使用 revoke-full shell ...
- [剑指Offer]42-连续子数组的最大和(DP)
题目链接 https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&tqId=11183&t ...