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. 1 <= A.length <= 5000
  2. 0 <= 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 【LEETCODE】42、922. Sort Array By Parity II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  8. 【Leetcode_easy】922. Sort Array By Parity II

    problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...

  9. 992. Sort Array By Parity II - LeetCode

    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...

随机推荐

  1. Rx操作符

    [Rx操作符] 1.Observable.from()方法,它接收一个集合作为输入,然后每次输出一个元素给subscriber: Observable.from("url1", & ...

  2. 外购半成品回写PR时将同一供应商同一编码的PR合并数量回写

    '); --PR 净需求 '); ---加上PR回写逻辑后 '); ---加上PR回写逻辑后 ') order by item; ---最终回写去SAP的数据 ) as LGORT ,'SAPRFC' ...

  3. html position定位

    一.fixed居中 css样式代码:{ position:fixed left: 0; right: 0; margin:0 auto; width:300px } 二.Position属性有四个值: ...

  4. dedecms 后台修改系统设置,但是config.cache.inc.php文件不能写入

    fopen居然返回false,既不是目录或文件权限相关问题,也不是文件路径问题(相对路径.绝对路径)等,更不是打开文件的模式问题(r,w,a等).网上搜了一会,说到返回false的原因无非都是上面三种 ...

  5. 进程实时监控pidstat命令详解

    pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存.设备IO.任务切换.线程等.pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上次运行 ...

  6. Array 遍历数组

    public static void main(String args){ int a[][] = new int[3][4]; for(int i=0;i<a.length;i++){ for ...

  7. 《纪念碑谷》(Monument Valley) 系列游戏的空间结构是如何设计的?

    这个游戏有着像埃舍尔的画一样有着空间结构的矛盾,如果是2D游戏,艾达可以行走的路线.建筑结构变化的过程是如何绘制的?如果是3D游戏,那么如何对这种违背物理定律的矛盾空间进行建模?以前有一个叫“无限回廊 ...

  8. Wannafly挑战赛14 C.可达性(tarjan缩点)

    题目描述 给出一个 0 ≤ N ≤ 105 点数.0 ≤ M ≤ 105 边数的有向图, 输出一个尽可能小的点集,使得从这些点出发能够到达任意一点,如果有多个这样的集合,输出这些集合升序排序后字典序最 ...

  9. [剑指Offer]判断一棵树为平衡二叉树(递归)

    题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...

  10. 解决在Mac的Vmware Fusion中装win7系统和mac原生系统直接切换win7系统分辨率变化的问题

    虚拟机 - 设置 - 显示屏 - 全屏显示retina (此选项钩去掉)