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. 清除冗余的css

    下载旧版的火狐浏览器,如Firefox 48.0.exe, 下载地址:https://ftp.mozilla.org/pub/firefox/releases/48.0/win32/zh-CN 关闭更 ...

  2. 简单的Java ee思维导图

  3. HTTP与HTTPS有哪些区别?

    大纲 这里写图片描述一.前言: 这里写图片描述这里写图片描述先来观察这两张图,第一张访问域名http://www.12306.cn,谷歌浏览器提示不安全链接,第二张是https://kyfw.1230 ...

  4. JAVAEE 第七周

    JSON语法: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集 ...

  5. jar包添加到maven本地仓库

    操作系统windows 本地要配置过maven环境 cmd  运行命令 mvn install:install-file -Dfile=D:\commons-net-3.6.jar.jar -Dgro ...

  6. 慢阻肺疾病管理APP——第一次迭代心得

    时光匆匆,不知不觉就到了第十二周.——第一次迭代都完成了,最终迭代还会远吗? 一.第一次迭代的设想和目标: 1.  我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? ...

  7. 为什么使用zookeeper?

    随着应用规模的迅速扩张,单台机器的部署已经难以支撑用户大规模.高并发的请求了, 因此服务化.集群化.分布式概念应运而生. 针对这种场景,人们通常使用的做法就是将软件按照模块进行拆分,形成独立的子系统, ...

  8. npm run dev 报错 run `npm audit fix` to fix them, or `npm audit` for details

    前几天写的直接运行npm run dev还是ok的,突然不行了,前面报错是css-loader没有,删除style标签上的lang='scss'就好了,先不需要这个依赖.这个先不管. 只是后面的 ru ...

  9. selenium3 - Tomcat and jenkins

    一.Tomcat的学习 1.tomcat 下载地址:tomcat.apache.org 2.配置tomcat环境变量: 新建系统变量 -  变量名:CATALINA_HOME    变量值:D:\TO ...

  10. makefile笔记9 - makefile隐含规则

    在我们使用 Makefile 时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix 下是[.o]文件,Windows 下是[.obj]文件). ...