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. Java实现post和get请求

    GET请求:GET请求会向服务器发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行 ...

  2. 关于sql注入漏洞的挖掘及渗透工具简介

    大量的现代企业采用Web应用程序与其客户无缝地连接到一起,但由于不正确的编码,造成了许多安全问题.Web应用程序中的漏洞可使黑客获取对敏感信息(如个人数据.登录信息等)的直接访问. Web应用程序准许 ...

  3. 使用 spring封装的javamail linux服务器发送邮件失败解决

    原文参考:https://blog.csdn.net/a540891049/article/details/79385471 由于某些平台的linxu服务器为了安全起见 屏蔽了发送邮件的常用端口 25 ...

  4. 按住CTRL多选,按住shift连选的实现

    <tr class="address" v-for="(counts, index) in counts" :key="index" ...

  5. Collections集合工具类

    一.Collection与Collections Collection 是所有单列集合的根接口 Collection 是操作集合的工具类 二.Collections中常见的方法:(大都是static方 ...

  6. 构建Spring Cloud微服务分布式云架构

    大型企业分布式微服务云架构服务组件 实现模块化.微服务化.原子化.灰度发布.持续集成 commonservice zipkinSpring 日志收集工具包,封装了Dapper和log-based追踪以 ...

  7. Hacker一月间

    我给自己的新赛季制定了一个计划,就是研究Kali,一个大集成开源软件系统,不过是用来做Hacker的. 以前其实想玩玩这个,但负责信息安全,总是担心安全漏洞这里安全漏洞哪儿,其实自己很害怕有安全漏洞, ...

  8. JS-斜杠和反斜杠的转换

    例子:var url = "http://localhost:64177/Home/AccordionIndex"; 将斜杠转换成反斜杠: url = url .replace(& ...

  9. [Java] [Singleton] [DCL][happens-before]

    Singleton 只能有一个实例:必须自己创建自己的实例:必须给其他所有对象提供这一实例 实现方法 饿汉式singleton 预先加载法 class Single { private Single( ...

  10. EditPlus软件自动补全文档htmlbar.acp设置 及 模板文件格式

    1.在htmlbar.acp文件末尾添加如下内容,可自动补全: #T=HTML <html>    ^! </html>   #T=HEAD <head>    ^ ...