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. linux 6.5上创建新用户后,不能登陆?

    linux 6.5上创建新用户后,不能登陆? 使用root账户登陆却可以! [root@ log]# useradd mtdk[root@ log]# echo 123abc |passwd --st ...

  2. mongodb 超出内存限制

    mongodb 查询数据默认占用最大内存为100M,如果查询涉及到大量数据进行$group.$sort时会抛出类似以下的异常: Received error :: { $err: \"Exc ...

  3. solr6.4.1 搜索引擎(1)启动eclipse启动

    solr是一个java写的搜索引擎,所以支持java方式的eclipse调试. 本篇文章使用solr版本为6.4.1 一. 环境 solr 下载地址 http://archive.apache.org ...

  4. centos7 安装gitlab任意版本

    主要还是根据官网:https://www.gitlab.cc/installation/#centos-7 1.安装依赖: sudo yum install curl policycoreutils ...

  5. Sklearn (一) 监督学习

    本系列博文是根据SKlearn的一个学习小结,并非原创!  1.直接学习TensorFlow有点不知所措,感觉需要一些基础知识做铺垫.  2.之前机器学习都是理论<Ng机器学习基础>+底层 ...

  6. redis-缓存失效三种策略(FIFO 、LRU、LFU)

    缓存失效策略(FIFO .LRU.LFU三种算法的区别) 当缓存需要被清理时(比如空间占用已经接近临界值了),需要使用某种淘汰算法来决定清理掉哪些数据.常用的淘汰算法有下面几种: FIFO:First ...

  7. python函数作用域,嵌套函数,闭包

    函数作用域                                                                                                ...

  8. json&pickle&shelve模块

    之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了 ...

  9. C#简繁体转换

    /// <summary>/// 字符串简体转繁体/// </summary>/// <param name="strSimple"></ ...

  10. Kubelet bootstrap 流程

    首先,什么是kubelet bootstrap?在安装 k8s worker node 时,基本上 worker 的初始状态仅仅是安装了 docker 和 kubelet,worker 需要一种机制跟 ...