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. 洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows 题解

    P2858 [USACO06FEB]奶牛零食Treats for the Cows 题目描述 FJ has purchased N (1 <= N <= 2000) yummy treat ...

  2. 2017.10.1 国庆清北 D1T1 zhx的字符串题

    题目背景 2017国庆清北D1T1 题目描述 你是能看到第一题的 friends 呢. ——hja 何大爷对字符串十分有研究,于是天天出字符串题虐杀 zhx.何大爷今天为 字符串定义了新的权值计算方法 ...

  3. hhhhh臭不要脸//捂脸)多不好意思啊you进步惹

    如题↑↑↑ 千万不要相信题目 还是看图说话吧↓↓↓ 我真的蒟蒻啊,,,准确率在70边缘徘徊,卑微☹ 不过还是侥幸地进入了前 30 名! 今天七夕欸,然鹅,,, qq空间里面弥漫着恋爱的酸臭味 香气,‘ ...

  4. webpack系列之安装(Mac OS)

    1. webpack介绍,可参考Webpack中文文档 2. 安装webpack之前先需要安装npm,可参看NPM的使用介绍 3. 安装webpack,可参考Webpack入门教程 ========= ...

  5. Tcl条件语句

    If {条件表达式1} { 执行语句1 } elseif {条件表达式2} { 执行语句2 } elseif {条件表达式3} { 执行语句3 } else { 执行语句4 } 注:elseif {条 ...

  6. SDN第六次上机作业

    1.实验拓扑 实验拓扑图如下: 搭建代码如下: 创建py脚本文件,并编写代码,如下: class MyTopo(Topo): def __init__(self): # initilaize topo ...

  7. go 练习:HTTP 处理

    这篇文章只是联系go指南时的笔记吧. package main import ( "fmt" "log" "net/http" ) type ...

  8. unity2019新建LWRP项目出错:Failed to resolve project template

    原因不详,但是在C盘创建项目的确会出现这个问题,改到D盘或E盘就没这个问题了

  9. Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)

    public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...

  10. IDEA2019.2中文字体变粗缺字等问题

    idea的中文字体渲染问题 IDEA 2018.2升级到 IDEA 2019.2,中文字体渲染问题修改一下备用字体就可以共需要修改两处:1.Setting -> Editor -> Fon ...