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. SoapUI之cookie设置

    一.测试背景: 1)接口测试需要完成注册-->登录-->充值,使用soapui构建好测试用例.设置断言后,运行结果如下: 2)recharge接口运行失败,继续查看该接口具体发送的请求及返 ...

  2. 使用vagrant构建你们团队的开发环境

    vagrant可以让团队快速搭建统一的开发环境. 搭建vagrant你需要准备三个东西: 1.vagrant安装包 . 2.virtualbox安装包. 3.打包后的vagrant虚拟环境镜像 (ln ...

  3. 用docker搭建python项目运行环境

    Docker Hub镜像加速器 安装docker: curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-eng ...

  4. tensorflow 只恢复部分模型参数

    import tensorflow as tf def model_1(): with tf.variable_scope("var_a"): a = tf.Variable(in ...

  5. FX-玩列表

    list = []while True: meus = ("1.查看","2.添加","3.删除","0.退出") pr ...

  6. Apache安装,亲测成功

    工作需要,为一台空白服务器安装apache,小白程序员,搞了一个下午,惭愧! 工具需要,也可以自己到apache下载 http://httpd.apache.org/download.cgi 遇到的b ...

  7. 简单的Java ee思维导图

  8. python机器可读数据-json

    导入JSON数据相对简单.下面为打开.加载.导入.输出的操作. import json data = open('data.json').read() data = json.loads(data) ...

  9. linux 迁移项目ProtocolException

    背景:服务器跟换机房,虚拟机完整迁移项目,只修改ip和主机名 1.检查/etc/hosts 中ip 和主机名映射 2.检查网络端口是否有限制以及端口开放是否全了,检查ip有没有配对.RMI注册不上.

  10. ALV屏幕捕捉回车及下拉框事件&ALV弹出框回车及下拉框事件

    示例展示: 屏幕依据输入的物料编码或下拉框物料编码拍回车自动带出物料描述: 点击弹出框,输入物料编码拍回车带出物料描述,点击确认,更新ALV: 1.创建屏幕9000,用于处理ALV弹出框: 2.针对屏 ...