【LEETCODE】42、922. Sort Array By Parity II
- 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的更多相关文章
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 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 ...
- [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 ...
- #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 ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【leetcode】922. Sort Array By Parity II
题目如下: 解题思路:非常简单的题目,引入两个变量oddInx = 1和evenInx = 0,和与A等长的结果数组res.然后遍历A,如果A[i]为偶数,则令res[evenInx] = A[i], ...
- 【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. ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 查看.NET应用程序中的异常(下)
为什么要使用内存转储进行调试? 在两种主要情况下,您可能需要使用内存转储进行调试.第一种情况是应用程序有一个未处理的异常并崩溃,而您只有一个内存转储.第二种情况是,在生产环境中出现异常或特定行为,并且 ...
- CSS块元素、行内元素、行内块元素的转换
一.块元素转行内元素:display:inline 二.行内元素转块元素:display:block div{ display: inline; /*无效 width: 500px; height: ...
- linux date获取时间戳
linux 时间戳格式 年月日 时分秒: `date ‘+%Y%m%d%H%M%S’`date +%Y%m%d%H%M%S // 年月日 时分秒date +%s // 从 1970年1月1日零点开始到 ...
- 限流神器之-Guava RateLimiter 实战
前段时间,项目中需要对某些访问量较高的路径进行访问并发数控制,以及有些功能,比如Excel导出下载功能,数据量很大的情况下,用户不断的点击下载按钮,重复请求数据库,导致线上数据库挂掉.于是在这样的情况 ...
- php保存canvas导出的base64图片
代码如下: <?php $img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABxCAYAAABoUdWRAAAAAXNSR0IAr ...
- 手动停止jquery ajax请求
主要调用jquery提供的ajax abort方法,详细代码如下: <html> <head> <meta charset="UTF-8"> & ...
- 【2019.11.27】SDN上机第5次作业
参考资料: https://www.cnblogs.com/zzqsss/p/11924685.html 问答环节 描述官方教程实现了一个什么样的交换机功能? Ryu是一个基于组件的软件定义的网络框架 ...
- hangfire控制台应用程序中添加控制面板
1.使用nuget 管理包安装 Microsoft.AspNet.WebApi.OwinSelfHost 2.根目录添加新建类 名为:Startup.cs public class Startup { ...
- SQLSERVER根据提成比率区间计算业绩提成
USE [Employee] GO /****** Object: Table [dbo].[Commission] Script Date: 2019/11/17 14:10:21 ******/ ...
- 第2课第6节_Java面向对象编程_包和权限_P【学习笔记】
摘要:韦东山android视频学习笔记 1.使用package定义编译的时候存放的位置 package a.b.c.d; public class Package { public static v ...