【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 * ...
随机推荐
- 2019qbxt游记
Day 1 2019.8.6 来到qbxt的第一天,虽然早就对宾馆的等级做好了准备,但是还是十分的失望,外观是真的很简陋,不过里面还好的,,可以凑合. 我竟然和lbh一个宿舍!!!这次外出学习必将不安 ...
- 【loj2341】【WC2018】即时战略
题目 交互题: 一开始所有点都是黑的,你需要把所有点变白: explore(u,v)会将u到v路径上的第二个点变白: 一开始只有1号点是白色的,你需要让所有点变白: 对于一条链次数限制\(O(n+lo ...
- mac 下的 tree 命令 终端展示你的目录树结构
相信很多使用过Linux的用户都用过tree命令,它可以像windows的文件管理器一样清楚明了的显示目录结构.mac 下使用 brew包管理工具安装 tree 前提:安装了homebrew(安装指令 ...
- [后渗透]Metasploit使用基础
0x00 简介 Metasploit是一个免费的.可下载的框架,通过它可以很容易地获取.开发并对计算机软件漏洞实施攻击.它本身附带数百个已知软件漏洞的专业级漏洞攻击工具.当H.D. Moore在200 ...
- 【大数据应用技术】作业十一|分布式并行计算MapReduce
本次作业在要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapRe ...
- Web前端开发规范 之html命名规范
1.文件名称命名规则 统一用小写的英文字母.数字和下划线,不得包含汉字空格和特殊符号 2.索引文件命名 一般用index为名字 如index.html index.jsp 3.各子页面的命名规则 ...
- uniapp - 键盘弹起背景图片不会被挤压
[释义] uni.getSystemInfoSync()获取屏幕可用高度windowScreen做为背景图高度即可(非虚拟DOM也可以使用本思路). [源码] <template> < ...
- 为什么使用css3和div布局?
1,代码精简(没有本身自带的一些属性,容易设置样式)2,解决了table表格的嵌套问题3,速度问题(页面代码减少,增加了编写代码的速度)4 ,对排名的影响,基于xhtml标准的div+css布局会更快 ...
- SymPy解方程的实现
https://www.cnblogs.com/zgyc/p/6277562.html SymPy完全是用Python写的,并不需要外部的库 原理: 单纯用语言内置的运算与变量解决的是,由值求结果.如 ...
- pycharm使用(持续更新)
1.Anaconda版本下载 https://repo.continuum.io/archive/