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. django models实际操作中遇到的一些问题

    问题1.将主键id改成自动生成的python3 manage.py migrate时报下面的错误 django.db.utils.InternalError: (1091, "Can't D ...

  2. Tableau-安装的坑

    前言: 为了学习Tableau的教程,我下载了这个软件从官网,结果安装的时候一直报一个奇怪的错误, 由于当时没有截图,只记得错误代码Xo80076666好像是,提示安装失败,已经有另一个产品安装 在我 ...

  3. ubuntu后台运行命令

    参考 https://blog.csdn.net/shaozg168/article/details/6979337 nohup ./test.sh &

  4. 用photoshop 把视频镜头做成GIF图片

    https://jingyan.baidu.com/article/47a29f2432e113c0142399b0.html

  5. js实现右击

    <!DOCTYPE html> <html>     <head>  <meta charset="UTF-8">  <tit ...

  6. 相对熵(KL散度)

    https://blog.csdn.net/weixinhum/article/details/85064685 上一篇文章我们简单介绍了信息熵的概念,知道了信息熵可以表达数据的信息量大小,是信息处理 ...

  7. Ubuntu虚拟机全屏问题

    方法一:修改分辨率 xrandr查看能用的 xrandr -s 1920x1200改变. 方法二:安装新vmtools apt-get install open-vm-tools apt-get in ...

  8. Django的Rbac介绍3

    今天的博客主要是记录一下如何实现左侧菜单,这里我们想实现的效果就是,如果用户有查看用户的权限,则显示查看用户的左侧菜单,如果用户有查看角色的权限,则显示查看角色的左侧菜单,如果两者都有,则需要显示两个 ...

  9. Mapped Statements collection already contains value for*

    检查了一下,没有重复的,参数也都正确,把报错的地方注释掉继续报下一个方法错误.重启也无效 最后发现,最后一个方法的返回值类型resultType="java.util.Map"写成 ...

  10. cloud配置中心遇到的坑

    https://blog.csdn.net/z960339491/article/details/80593982分布式配置中心为什么要有用分布式配置中心这玩意儿?现在这微服务大军已经覆盖了各种大小型 ...