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. C++ 类中 关于常量定义 理解总结

    前言 有时我们希望某些常量只在类中有效.由于#define定义的宏常量是 全局 的,不能达到目的,于是想当然地觉得应该用 const修饰 数据成员来实现.const数据成员的确是存在的,但其含义却不是 ...

  2. springboot如何通过apollo动态去注册dubbo服务

    参考相关文章: apollo官方文档:  https://dubbo.apache.org/zh/docs/v2.7/user/configuration/configuration-load-pro ...

  3. Git撤销、回滚操作

    git的工作流 工作区:即自己当前分支所修改的代码,git add xx 之前的!不包括 git add xx 和 git commit xxx 之后的. 暂存区:已经 git add xxx 进去, ...

  4. LiteFlow 2.6.4版本发行注记,里程碑版本!

    一 这个版本做的很折腾.期间几个issue推翻重做了好几次. 但我最终还是带来了LiteFlow 2.6.4这个重要版本. 虽然版本是小版本号升级,但是带来的更新可一点也不少.并完全向下兼容. 如果你 ...

  5. asp.net中挺高性能的24种方法

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  6. [bzoj5510]唱跳rap和篮球

    显然答案可以理解为有(不是仅有)0对情况-1对情况+2对情况-- 考虑这个怎么计算,先计算这t对情况的位置,有c(n-3t,t)种情况(可以理解为将这4个点缩为1个,然后再从中选t个位置),然后相当于 ...

  7. Java-ASM框架学习-java概念转字节码概念

    前言 当我们操作字节码的时候,都是和字节码的概念打交道,这让我们很困扰,asm也想到了这点,为了方便,它提供了一个可以把java概念转化为字节码概念的类 import org.objectweb.as ...

  8. 【TcaplusDB知识库】如何部署TcaplusDB Local 版

    [TcaplusDB知识库]部署TcaplusDB Local 版的准备操作 1. 版本介绍 TcaplusDB Local版,是为用户提供的一个满足本地开发调试的版本(基于Docker部署的可下载版 ...

  9. Ubuntu 软件安装

    apt 使用apt安装,需要sudo 一些命令: sudo apt-get install git deb deb软件安装方法: sudo dpkg -I xxxx.deb 我们在Windows下安装 ...

  10. Java 代码审计 — 1. ClassLoader

    参考: https://www.bilibili.com/video/BV1go4y197cL/ https://www.baeldung.com/java-classloaders https:// ...