package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: SortArrayByParityII
* @Author: xiaof
* @Description: 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.
*
* 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.
*
* 有一个数组A,其中奇元素和偶元素的数量相等。请把A排序,使得奇元素位于奇数位置,偶元素位于偶数位置。任何满足上述条件的排序都是合法的。
*
* @Date: 2019/7/3 17:54
* @Version: 1.0
*/
public class SortArrayByParityII { public int[] solution(int[] A) {
//定义2个索引,第一指向奇数索引,第二个偶数索引
int oddIndex = 1;
int evenIndex = 0;
while(oddIndex < A.length && evenIndex < A.length) {
while(oddIndex < A.length && (A[oddIndex] & 1) == 1) {
oddIndex += 2;
} while (evenIndex < A.length && (A[evenIndex] & 1) == 0) {
evenIndex += 2;
} //交换位置
if(oddIndex < A.length && evenIndex < A.length) {
int temp = A[oddIndex];
A[oddIndex] = A[evenIndex];
A[evenIndex] = temp;
oddIndex += 2;
evenIndex += 2;
}
} return A;
} public static void main(String args[]) {
int A1[] = {2,3};
SortArrayByParityII fuc = new SortArrayByParityII();
System.out.println(fuc.solution(A1));
} }

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

  1. 【LEETCODE】41、905. Sort Array By Parity

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

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

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

  3. 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 ...

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

  5. #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 ...

  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. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

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

随机推荐

  1. 关于新版本cube.js 集成preosto 的一个说明

    实际上cubejs cli 是可以使用的,只是官方文档对于dbtype 说明的问题 使用如下命令我们可以创建prestodb 的基本项目   cubejs create -d prestodb myd ...

  2. Log4net 控制台打印日志(二)

    1.创建控制台程序 2.用NuGet添加log4net引用 3.添加应用程序配置文件:App.config 4.添加配置信息: <?xml version="1.0" enc ...

  3. 洛谷 P4316绿豆蛙的归宿

    题目描述 记f[i]表示经过i号点的概率. 那么点v从点u到达的概率=经过点u的概率/点u的出度.由于v可以由多个点走到,所以f[v]+=f[u]/out[u]. 计算f的过程可以在拓扑中完成,同时可 ...

  4. 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛

    Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...

  5. 前端微信小程序仿菜谱精灵

    需求描述及交互分析 设计思路和相关知识点 底部标签导航设计 幻灯片轮播效果设计 菜谱专题列表显示设计 菜谱专题详情设计 菜谱分类设计 幻灯片轮播效果动态切换展示一些美食图片 若本号内容有做得不到位的地 ...

  6. 第12组 Beta冲刺(2/5)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 由于这两天在修严重Bug,故项目没有新的进展,燃尽图没有变化 展示Git当日代码/文档签入记录(组内 ...

  7. TP5验证码上传阿里云万网虚拟主机后,验证码不显示的解决办法

    TP5不显示验证码 清除缓冲区就应该可以了,今天我刚好也遇到了,解决的办法是在vendor/topthink/think-captcha/CaptchaController.php中加上这个ob_cl ...

  8. 解决MySql ERROR 1698 (28000) 错误:Access denied for user 'root'@'localhost'

    今天尝试在Ubuntu虚拟机上安装MySql 数据库(版本是:5.7.23-0Ubuntu0.18.04.1),数据库安装很简单,就是三行命令:   sudo apt-get install mysq ...

  9. Calcite分析 - Rule

    Calcite源码分析,参考: http://matt33.com/2019/03/07/apache-calcite-process-flow/ https://matt33.com/2019/03 ...

  10. Nginx Windows版安装及域名绑定

    1.到 http://nginx.org/en/download.html 下载一个稳定版本1.16.1 2.解压,放到C盘根目录下,如C:\nginx-1.16.1,此时双击nginx.exe就启动 ...