Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.

You may return any answer array that satisfies this condition.

Example 1:

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Note:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 0 <= A[i] <= 1000

Idea 1. Similar to Sort Array By Parity LT905, assume the array is in the order, what to do with next element?

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; even< A.length; even += 2) {
if(A[even]%2 == 1) {
while(odd < A.length && A[odd]%2 == 1) {
odd += 2;
}
swap(A, even, odd);
}
} return A;
}
}
 class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; even < A.length; even +=2) {
if((A[even]&1) == 1) {
while((A[odd]&1) == 1) {
odd += 2;
}
swap(A, even, odd);
}
} return A;
}
}

Idea 1.a two pointers walking towards each other

 class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; odd < A.length && even < A.length;) {
if(A[even]%2 == 1&& A[odd]%2 == 0) {
swap(A, even, odd);
}
if(A[even]%2 == 0) {
even +=2;
}
if(A[odd]%2 == 1) {
odd += 2;
}
} return A;
}
}

use a&1 == 1 instead of a%2 == 1 to check parity

 class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; odd < A.length && even < A.length; ) {
if((A[even]&1) == 1 && (A[odd]&1) == 0) {
swap(A, even, odd);
}
if((A[even]&1) == 0) {
even += 2;
}
if((A[odd]&1) == 1) {
odd += 2;
}
} return A;
}
}

Sort Array By Parity II LT922的更多相关文章

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

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

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

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

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

  4. 992. Sort Array By Parity II - LeetCode

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

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

  6. [Swift]LeetCode922.按奇偶排序数组 II | 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 ...

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

  8. #Leetcode# 922. Sort Array By Parity II

    https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...

  9. leetcode922 Sort Array By Parity II

    """ Given an array A of non-negative integers, half of the integers in A are odd, and ...

随机推荐

  1. Mysql建了索引查询很慢

    遇到一个问题,有几个结构一个的查询,表的索引建的也一样,但是有的查询很快,有的却很慢,需要半分钟以上才能执行完. 查看执行计划,并没有什么区别.找了很久原因才发现是主查询和子查询所涉及的表的字符编码不 ...

  2. PAT 乙级 1086 就不告诉你 (15 分)

    1086 就不告诉你 (15 分) 做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三.”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积. 输入格式 ...

  3. CSS之padding&margin

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. ubuntu crontab python 定时任务备记

    crontab -e 写入: # at a.m every week with: # * * tar -zcf /var/backups/home.tgz /home/ # # For more in ...

  5. 【Linux】【Jenkins】代码编译和执行过程中的问题汇总

    1.问题1:java.io.FileNotFoundException: /root/.jenkins/workspace/Videoyi_AutoTest_Maven/config-log4j\lo ...

  6. 堆排序(Heapsort)

    class Program { static void Main(string[] args) { , , , , , ,}; int size = arr.Length; Heapsort(arr, ...

  7. win10 linux 子系统 所在 目录

    C:\Users\用户名\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\r ...

  8. windows清空电脑的DNS缓存

    清空电脑的DNS缓存 按"Win+R"系统热键打开"运行"窗口,进入terminal终端 输入"ipconfig /flushdns"命令后 ...

  9. html: 仿制soundmanager2右上角面板

    仿制 http://schillmania.com/projects/soundmanager2/#volume 右上角面板 <style type="text/css"&g ...

  10. Maven下CXF的wsdl2java生成客户端代码

    <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...