【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 * ...
随机推荐
- rustup 使用
rustup 可以帮助我们安装不同版本的rust 编程需要的工具连,同时可以方便的进行不同版本 之间的切换,类似nodejs 的nvm,n, ruby 的 rvm python 的 venv ... ...
- 通过HttpServletRequest重写+filter 添加header
问题说明 需要做的事情比较简单,就是通过filter 重写httpservletrequest ,同时给予request 添加header 主要是通过HttpServletRequestWrapper ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- Python之NumPy(axis=0/1/2...)的透彻理解
https://blog.csdn.net/sky_kkk/article/details/79725646 numpy中axis取值的说明首先对numpy中axis取值进行说明:一维数组时axis= ...
- 坑爹微信之读取PKCS12流时出现的java.io.IOException: DerInputStream.getLength
背景 微信退款接口需要使用到证书,我参考微信的官方Demo进行,部分代码如下: char[] password = config.getMchID().toCharArray(); InputStre ...
- RocketMq重复消费问题排查
前情 出现了重复消费的问题,同一个消息被重复消费了多次,导致了用户端收到了多条重复的消息,最终排查发现,是因为消费者在处理消息的方法onMessage中有异常没有捕获到,导致异常上抛,被consume ...
- arts lettcode 题目
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- Monkey框架(基础知识篇) - monkey启动与参数介绍
一.monkey启动 直接PC启动:> adb shell monkey [options] <count> shell 端启动:> adb shell >monkey ...
- ubuntu之路——day15.1 只用python的numpy在底层检验参数初始化对模型的影响
首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...
- 第06组 Beta版本演示
队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/12052263.html 作业博客 : https://edu.cnblogs.com/campus ...