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. [转帖][分享] 关于系统DIY--by 原罪

    http://wuyou.net/forum.php?mod=viewthread&tid=399277&extra=page%3D1 前几天我发了一个帖子<Windows组件w ...

  2. centos6.5虚拟机无法访问外网解决办法

    安装了centos6.5虚拟机,使用的是桥接方式.把所有的配置已经写到/etc/sysconfig/network-scripts/ifcfg-eth0中后,发现内网可以ping通,外网却无法访问. ...

  3. Kafka 如何读取offset topic内容 (__consumer_offsets)(转发)

    原文  https://www.cnblogs.com/huxi2b/p/6061110.html 众所周知,由于Zookeeper并不适合大批量的频繁写入操作,新版Kafka已推荐将consumer ...

  4. Windows下,python pip安装时ReadTimeoutError解决办法

    一般情况下PIP出现ReadTimeoutError都是因为被GFW给墙了,所以一般遇到这种问题,我们可以选择国内的镜像来解决问题. 在Windows下: C:\Users\Administrator ...

  5. 5-安装sqoop

    1.解压,修改权限 sudo tar -zvxf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz -C /opt/app/ sudo chown -R hadoo ...

  6. dubbo配置方式简单介绍

    原地址:http://www.cnblogs.com/chanshuyi/p/deep_insight_dubbo_config.html 一.介绍 Dubbo 采用全Spring配置方式,透明化接入 ...

  7. json字符串装List<Object>

    List<SearchParam> ts = (List<SearchParam>) JSONArray.parseArray(jsonStr, SearchParam.cla ...

  8. orcal安装

    1.下载安装包(版本32位或64位)下载网址:https://www.oracle.com,建议关闭防火墙(可以的话关闭网) 2.将两个安装包解压为一个安装包 3.点击执行 3. 4. 5. 5. 6 ...

  9. PHP/TP5 接口设计中异常处理

    PHP提供 Exception 类来处理异常 new Exception('错误信息(默认为空)','错误代码(默认0)','异常链中前一个异常') 然后可以通过 e -> getMessage ...

  10. Python代码教你批量将PDF转为Word

    很多时候在学习时发现许多文档都是PDF格式,PDF格式却不利于学习使用,因此需要将PDF转换为Word文件,但或许你从网上下载了很多软件,但只能转换前五页(如WPS等),要不就是需要收费,那有没有免费 ...