1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array
  6. * @ClassName: SortArrayByParityII
  7. * @Author: xiaof
  8. * @Description: 922. Sort Array By Parity II
  9. * Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
  10. * Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
  11. * You may return any answer array that satisfies this condition.
  12. *
  13. * Input: [4,2,5,7]
  14. * Output: [4,5,2,7]
  15. * Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
  16. *
  17. * 有一个数组A,其中奇元素和偶元素的数量相等。请把A排序,使得奇元素位于奇数位置,偶元素位于偶数位置。任何满足上述条件的排序都是合法的。
  18. *
  19. * @Date: 2019/7/3 17:54
  20. * @Version: 1.0
  21. */
  22. public class SortArrayByParityII {
  23.  
  24. public int[] solution(int[] A) {
  25. //定义2个索引,第一指向奇数索引,第二个偶数索引
  26. int oddIndex = 1;
  27. int evenIndex = 0;
  28. while(oddIndex < A.length && evenIndex < A.length) {
  29. while(oddIndex < A.length && (A[oddIndex] & 1) == 1) {
  30. oddIndex += 2;
  31. }
  32.  
  33. while (evenIndex < A.length && (A[evenIndex] & 1) == 0) {
  34. evenIndex += 2;
  35. }
  36.  
  37. //交换位置
  38. if(oddIndex < A.length && evenIndex < A.length) {
  39. int temp = A[oddIndex];
  40. A[oddIndex] = A[evenIndex];
  41. A[evenIndex] = temp;
  42. oddIndex += 2;
  43. evenIndex += 2;
  44. }
  45. }
  46.  
  47. return A;
  48. }
  49.  
  50. public static void main(String args[]) {
  51. int A1[] = {2,3};
  52. SortArrayByParityII fuc = new SortArrayByParityII();
  53. System.out.println(fuc.solution(A1));
  54. }
  55.  
  56. }

【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. 查看.NET应用程序中的异常(下)

    为什么要使用内存转储进行调试? 在两种主要情况下,您可能需要使用内存转储进行调试.第一种情况是应用程序有一个未处理的异常并崩溃,而您只有一个内存转储.第二种情况是,在生产环境中出现异常或特定行为,并且 ...

  2. CSS块元素、行内元素、行内块元素的转换

    一.块元素转行内元素:display:inline 二.行内元素转块元素:display:block div{ display: inline; /*无效 width: 500px; height: ...

  3. linux date获取时间戳

    linux 时间戳格式 年月日 时分秒: `date ‘+%Y%m%d%H%M%S’`date +%Y%m%d%H%M%S // 年月日 时分秒date +%s // 从 1970年1月1日零点开始到 ...

  4. 限流神器之-Guava RateLimiter 实战

    前段时间,项目中需要对某些访问量较高的路径进行访问并发数控制,以及有些功能,比如Excel导出下载功能,数据量很大的情况下,用户不断的点击下载按钮,重复请求数据库,导致线上数据库挂掉.于是在这样的情况 ...

  5. php保存canvas导出的base64图片

    代码如下: <?php $img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABxCAYAAABoUdWRAAAAAXNSR0IAr ...

  6. 手动停止jquery ajax请求

    主要调用jquery提供的ajax abort方法,详细代码如下: <html> <head> <meta charset="UTF-8"> & ...

  7. 【2019.11.27】SDN上机第5次作业

    参考资料: https://www.cnblogs.com/zzqsss/p/11924685.html 问答环节 描述官方教程实现了一个什么样的交换机功能? Ryu是一个基于组件的软件定义的网络框架 ...

  8. hangfire控制台应用程序中添加控制面板

    1.使用nuget 管理包安装 Microsoft.AspNet.WebApi.OwinSelfHost 2.根目录添加新建类 名为:Startup.cs public class Startup { ...

  9. SQLSERVER根据提成比率区间计算业绩提成

    USE [Employee] GO /****** Object: Table [dbo].[Commission] Script Date: 2019/11/17 14:10:21 ******/ ...

  10. 第2课第6节_Java面向对象编程_包和权限_P【学习笔记】

    摘要:韦东山android视频学习笔记  1.使用package定义编译的时候存放的位置 package a.b.c.d; public class Package { public static v ...