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

这道题是之前那道 [Sort Array By Parity](https://www.cnblogs.com/grandyang/p/11173513.html) 的拓展,那道让把奇数排在偶数的后面,而这道题是让把偶数都放在偶数坐标位置,而把奇数都放在奇数坐标位置。博主最先想到的方法非常简单粗暴,直接分别将奇数和偶数提取出来,存到两个不同的数组中,然后再把两个数组,每次取一个放到结果 res 中即可,参见代码如下:


解法一:

class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> res, even, odd;
for (int num : A) {
if (num % 2 == 0) even.push_back(num);
else odd.push_back(num);
}
for (int i = 0; i < even.size(); ++i) {
res.push_back(even[i]);
res.push_back(odd[i]);
}
return res;
}
};

论坛上还有一种更加简单的方法,不需要使用额外的空间,思路是用两个指针,i指针一直指向偶数位置,j指针一直指向奇数位置,当 A[i] 是偶数时,则跳到下一个偶数位置,直到i指向一个偶数位置上的奇数,同理,当 A[j] 是奇数时,则跳到下一个奇数位置,直到j指向一个奇数位置上的偶数,当 A[i] 和 A[j] 分别是奇数和偶数的时候,则交换两个数字的位置,从而满足题意,参见代码如下:


解法二:

class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
int n = A.size(), i = 0, j = 1;
while (i < n && j < n) {
if (A[i] % 2 == 0) i += 2;
else if (A[j] % 2 == 1) j += 2;
else swap(A[i], A[j]);
}
return A;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/922

类似题目:

Sort Array By Parity

参考资料:

https://leetcode.com/problems/sort-array-by-parity-ii/

https://leetcode.com/problems/sort-array-by-parity-ii/discuss/181160/Java-two-pointer-one-pass-inplace

https://leetcode.com/problems/sort-array-by-parity-ii/discuss/193854/Linear-pass-using-2-pointers-in-C%2B%2B.

https://leetcode.com/problems/sort-array-by-parity-ii/discuss/181158/C%2B%2B-5-lines-two-pointers-%2B-2-liner-bonus

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二的更多相关文章

  1. Leetcode922.Sort Array By Parity II按奇偶排序数组2

    给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...

  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# 922. Sort Array By Parity II

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

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

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

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

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

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

  7. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

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

    题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...

  9. 【leetocde】922. Sort Array By Parity II

    Given an array of integers nums, half of the integers in nums are odd, and the other half are even.  ...

随机推荐

  1. python I/O复用

    select是阻塞式的方法

  2. gitLab 分支保护设置

    一.需求背景 开发当前开发的分支遇到暂时无法解决的问题,现在有需要开发其他应用,所以希望运维这边将当前有问题分支冻结,让其他人无法进行修改,待后续有时间在排查代码问题 二.Gitlab配置步骤 1.搜 ...

  3. Beats Elastic中的Auditbeat使用介绍

    Auditbeat使用介绍 Auditbeat是一种轻量级的数据收集器,您可以将其安装在服务器上,以审核系统上用户和进程的活动. 例如,您可以使用Auditbeat从Linux Audit Frame ...

  4. 2019-11-25-win10-uwp-发布旁加载自动更新

    原文:2019-11-25-win10-uwp-发布旁加载自动更新 title author date CreateTime categories win10 uwp 发布旁加载自动更新 lindex ...

  5. C#获取文件夹下所有的文件名称

    例如想获取后缀名为.txt的文件 //第一种方法 var files = Directory.GetFiles(path, "*.txt"); foreach (var file ...

  6. 纯 JS 设置文本框的默认提示

    HTML5 中有个新特性叫 placeholder,一般用它来描述输入字段的预期值,适用于 text.search.password 等类型的 input 以及 textarea.示例如下: < ...

  7. Scrapy-Splash简介及验证码的处理(一)

    目录 一:Splash简介与准备 二:验证码的识别(1)   在之前的博客中,我们学习了selenium的用法,它是一个动态抓取页面的方法,但是,动态抓取页面还有其他的方法,这里介绍Splash方法, ...

  8. mysql判断是否包含某个字符的方法

    mysql判断是否包含某个字符的方法用locate 是最快的,like 最慢.position一般实战例子:select * from historydatawhere locate('0',open ...

  9. 针对JCC指令练习的堆栈图

    堆栈图,主要目的就是练习一下JCC指令的熟练度,供参考 版权声明:本文为博主原创文章,转载请附上原文出处链接和本声明.2019-09-10,23:41:41.作者By-----溺心与沉浮----博客园 ...

  10. java Random类生成随机数

    封装一个方法: import java.util.Random; public class RandomUtil { /** * nextInt(num) 产生[0 ~ (num-1)]的随机数, 闭 ...