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. React 的坑

    MemoryRouter 会缓存组件,导致有时候 componentDidMount 不会执行

  2. Android Studio之SVN打分支、切换分支及合并分支

    1.打分支: 右击项目--Subversion--Branch or Tag 点击OK,分支就创建成功了,接下来我们切换到分支v2 2.切换分支: 右击项目--Subversion--Update D ...

  3. Dynamic learning rate in training - 培训中的动态学习率

    I'm using keras 2.1.* and want to change the learning rate during training. I know about the schedul ...

  4. 终端的rz命令,覆盖原文件。

    不覆盖:rz 覆盖 同名文件:rz -y

  5. 搭建本地yum源

    本地yum源其实非常容易搭建 首先进入/etc/yum.repos.d/ 将原来的yum源备份后移除,然后新建dvd.repo: 内容如下: [base] name=base baseurl=file ...

  6. Android : Camera2/HAL3 框架分析

    一.Android O上的Treble机制: 在 Android O 中,系统启动时,会启动一个 CameraProvider 服务,它是从 cameraserver 进程中分离出来,作为一个独立进程 ...

  7. ubuntu1604使用之旅——Qt交叉编译移植

    1.手头已有Qt-Embedded-5.7.0.tar.gz 2.解压 3.sudo cp Qt-Embedded-5.7.0 -r  /usr/local/ 4.sudo vim ~/.bashrc ...

  8. kali linux 配置嵌入式开发环境

    kali linux 2018.2 x64 一.支持i386库 如果你是64位的Kali Linux系统,用如下命令添加i386架构支持到你的开发环境. dpkg --add-architecture ...

  9. php字符串转数组

    下面代码包括了含有中文汉字的字符. function str2arr($str) { $length = mb_strlen($str, 'utf-8'); $array = []; for ($i= ...

  10. Hyperledger Fabric 架构梳理

    区块链的数据结构 State数据结构 由peer维护,key/value store Ledger  记录了所有成功和不成功的状态更新交易.Ledger被ordering service构造,是一个全 ...