Given an array of integers nums, half of the integers in nums are odd, and the other half are even. Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even.   Return any answer array that satisfies this condition. 

  slove it in place
  

class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
//在同一块区域进行遍历查询 双指针 一个只检查奇数位置 一个只检查偶数位置 然后二者进行交换 有点快排的思想
int n=nums.size();
int evenp=0,oddp=1;
while(evenp<n && oddp<n){
while(evenp<n && nums[evenp]%2==0){
evenp+=2;
}
while(oddp<n && nums[oddp]%2==1){
oddp+=2;
}
if(evenp<n && oddp<n){
int temp=nums[evenp];
nums[evenp]=nums[oddp];
nums[oddp]=temp;
evenp+=2;
oddp+=2;
}
}
return nums; }
};
 

【leetocde】922. Sort Array By Parity II的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode 922. Sort Array By Parity II C++ 解题报告

    922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...

  6. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  7. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

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

  9. 【LeetCode】905. Sort Array By Parity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:h ...

随机推荐

  1. poj 3041 Asteroids(最小点覆盖)

    题意: N*N的矩阵,有K个敌人,坐标分别是(C1,C1),.....,(Rk,Ck). 有一个武器,每发射一次,可消掉某行或某列上的所有的敌人. 问消灭所有敌人最少需要多少发. 思路: 二分建图:左 ...

  2. Linux 系统分区方案 详细教程

    简单分区方案 实际上,很多时候我们只需要分两个区:/和交换分区,日常使用基本不会有任何影响,甚至于交换分区对于现在的电脑来说都不是必要的,我们完全可以只分配一个根分区.linux只需要一个/根分区就可 ...

  3. SpringBoot 中发布ApplicationEventPublisher,监听ApplicationEvent 异步操作

    有这么一个业务场景:当用户注册后,发送邮件到其邮箱提示用户进行账号激活,且注册成功的同时需要赠送新人用户体验卡券. 业务有了,那么问题也就来了. What? 问题....问题?我听说你有问题? 来拔刀 ...

  4. 谷粒 | 项目集成redis

    添加依赖 由于redis缓存是公共应用,所以我们把依赖与配置添加到了common模块下面,在common模块pom.xml下添加以下依赖 <!-- redis --> <depend ...

  5. PTA 7-2 邻接表创建无向图 (20分)

    PTA 7-2 邻接表创建无向图 (20分) 采用邻接表创建无向图G ,依次输出各顶点的度. 输入格式: 输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数. 输 ...

  6. Part 39 AngularJS route change events

    In this video we will discuss1. Different events that are triggered when a route change occurs in an ...

  7. Python使用cx_Oracle模块操作Oracle数据库--通过sql语句和存储操作

    https://www.jb51.net/article/125160.htm?utm_medium=referral  Python使用cx_Oracle调用Oracle存储过程的方法示例 http ...

  8. 探究 Go 源码中 panic & recover 有哪些坑?

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/627 本文使用的go的源码1.17.3 前言 写这一篇文章的原因是 ...

  9. 关于【【故障公告】数据库服务器 CPU 近 100% 引发的故障(源于 .NET Core 3.0 的一个 bug)】IS NOT NULL测试

    测试如图,Core_Users的PhoneNumber可为空,存在索引,记录数1500000+ 增加is not null,查询计划消耗增加了一个0%的筛选器消耗,IO消耗如下一模一样 如果是IS N ...

  10. [luogu2303]Longge的问题

    1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 ll n,ans; 5 ll phi(l ...