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 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:

2 <= A.length <= 20000

A.length % 2 == 0

0 <= A[i] <= 1000

解题思想

  1. 使用 2 个 vector 分别保存奇数和偶数,最后依次在合起来。这个方法最直接。
    vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> odd;
vector<int> even; for(auto it = A.begin(); it != A.end(); it++) { // 偶数
if(*it % 2 == 0) {
even.push_back(*it);
}
// 奇数
else {
odd.push_back(*it);
}
} int i = 0, j = 0, cnt = 0, len = A.size(); A.clear(); for(;cnt<len;cnt++) {
// 奇数位置
if(cnt%2 != 0) {
A.push_back(odd[i++]);
}
// 偶数位置
else {
A.push_back(even[j++]);
}
} return A;
}
  1. 分别找偶数位置不是偶数,奇数位置不是奇数的地方进行互换。
    vector<int> sortArrayByParityII(vector<int>& A) {

        // 检查索引 i 和 A[i] 是否都是偶数
const auto checkeven = [&A](const int i)->bool {
return i%2==0 && A[i]%2==0;
}; // 检查索引 i 和 A[i] 是否都是奇数
const auto checkodd = [&A](const int i)->bool {
return i%2!=0 && A[i]%2!= 0;
}; // 找到索引 i 和A[i] 奇偶数不匹配的位置
const auto findIndex = [&A](int i, const auto func)->int {
while(i < A.size() && func(i) == true) {
i += 2;
} return i;
}; int even = findIndex(0,checkeven);
int odd = findIndex(1, checkodd); while(odd < A.size() && even < A.size()) {
swap(A[even], A[odd]); even = findIndex(even, checkeven);
odd = findIndex(odd, checkodd);
} return A;
}

LeetCode 922. Sort Array By Parity II C++ 解题报告的更多相关文章

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

  2. #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 ...

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

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

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

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

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

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

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

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

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

  8. 【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.  ...

  9. 992. Sort Array By Parity II - LeetCode

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

随机推荐

  1. flask 第七章 简陋版智能玩具 +MongoDB初识和基本操作

    1.简陋版web智能玩具 FAQ.py文件 import os from aip import AipSpeech, AipNlp from uuid import uuid4 "" ...

  2. 记一次用express手写博客

    1.req.session时一直是undefined 解决方法: // sesssion应用的配置 app.use(session({ secret:'blog', cookie: ('name', ...

  3. 响应式编程系列(一):什么是响应式编程?reactor入门

    响应式编程 系列文章目录 (一)什么是响应式编程?reactor入门 (二)Flux入门学习:流的概念,特性和基本操作 (三)Flux深入学习:流的高级特性和进阶用法 (四)reactor-core响 ...

  4. java_jsp和servlet中乱码问题

  5. python enumerate用法总结

    enumerate()说明enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumera ...

  6. 音频播放器在chrome浏览器,play报错

    这个问题是谷歌浏览器的问题:https://www.oschina.net/news/96168/chrome-remove-the-autoplay-policy,可以查看这篇文章 如果你的版本没有 ...

  7. docker配置代理的用户名密码

    公司访问外网全部需要经过代理服务器,在使用docker的过程中,发现就算为docker配置了代理,还是会因为没有代理服务器认证,导致pull操作失败,报如下错误: Error response fro ...

  8. 第四次Scrum编码冲刺

    第四次Scrum编码冲刺!!!! 一.总体任务: 本次冲刺是完成对图书馆管理系统的最后三个功能的实现------管理员对用户授权.用户注销和用户查询 二.个人任务及完成情况:    本人本次的任务是实 ...

  9. vs2017cpu占用过高解决方案

    最近在开发中,发现机器变得很卡顿.查看资源管理器发现vs的cpu使用率一直在20%-40%之间.占据了大量的系统计算资源. 展开资源管理器发现有很多node的线程,杀死后,他们又会自己起来! 一翻搜索 ...

  10. AGV

    AGV AGV是(Automated Guided Vehicle)的缩写,意即“自动导引运输车”,是指装备有电磁或光学等自动导引装置,它能够沿规定的导引路径行驶,具有安全保护以及各种移载功能的运输车 ...