905. Sort Array By Parity
Description
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
分析:
1、 题目要求将偶数排在奇数前面, 凡是返回的数组满足这个条件就行;
2、创建一个evenIndex用来标记当前最后一个偶数的下一个位置,遍历整个数组,当遇到偶数的时候,就和该位置的数交换,遍历完所有偶数就交换到了前面;
3、使用位运算判断奇数偶数,奇数的最低位是1,偶数的最低位是0,A[i] & 1这个操作就保留了A[i]的最低位,判断即可。
class Solution {
    public int[] sortArrayByParity(int[] A) {
        if(A.length == 0 || A.length == 1) {
            return A;
        }
        int evenIndex = 0;
        for(int i = 0; i < A.length; i++) {
            if((A[i] & 1) == 0) {
                int temp = A[i];
                A[i] = A[evenIndex];
                A[evenIndex] = temp;
                evenIndex++;
            }
        }
        return A;
    }
}
												
											905. Sort Array By Parity的更多相关文章
- LeetCode 905. Sort Array By Parity
		
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
 - 【LEETCODE】41、905. Sort Array By Parity
		
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
 - 【Leetcode_easy】905. Sort Array By Parity
		
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
 - 905. Sort Array By Parity - LeetCode
		
Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...
 - [LeetCode] 905. Sort Array By Parity 按奇偶排序数组
		
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
 - LeetCode 905 Sort Array By Parity 解题报告
		
题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...
 - [LeetCode&Python] Problem 905: Sort Array By Parity
		
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
 - LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
		
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
 - 【leetcode】905. Sort Array By Parity
		
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
 
随机推荐
- Hdoj 1176.免费馅饼 题解
			
Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...
 - mysql安转过程中出现的问题! Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exis
			
net start mysql启动失败,报错信息如上,因缺少mysql这个库 所以跳过 在my.ini中添加 --skip-grant-tables 再启动mysql 然后进入mysql 倒入一个从其 ...
 - Python变量与赋值
			
Python是一门独特的语言,与C语言有很大区别,初学Python很多萌新表示对变量与赋值不理解,学过C的都知道,给变量赋值时,需要先指定数据类型,同时会开辟一块内存区域,用于存储值,例如: int ...
 - 「JLOI2015」战争调度 解题报告
			
「JLOI2015」战争调度 感觉一到晚上大脑就宕机了... 题目本身不难,就算没接触过想想也是可以想到的 这个满二叉树的深度很浅啊,每个点只会和它的\(n-1\)个祖先匹配啊 于是可以暴力枚举祖先链 ...
 - A.01.09—模块的输出—PWM低端输出
			
PWM输出在汽车上的应用也比较多,它有三种不同的实现方式. 第一种由软件实现,即软件设定对一个输出口拉高和拉低的时间,形成时高时低的PWM控制:但这种方式目前用得不多,这是由使用需求和软件本身的特性决 ...
 - express搭建服务器
			
学习express搭建node服务器 一.安装express框架 1.了解框架(百度) 2.下载框架 (1)使用npm命令下载 npm install express -g //全局安装,安装的是ex ...
 - Arch更新时failed to prepare transaction
			
error: failed to prepare transaction (could not satisfy dependencies) :: ffmpeg2.8: installing x265 ...
 - 【CF1119D】Frets On Fire
			
题目大意:给定一个长度为 n 的序列,给定一个恒定的 w,求解 \[\sum\limits_{i=1}^{n}min\{d[i],w\}\] 题解:学会了对最小值和式的快速处理. 若在下标的角度考虑, ...
 - 详解 清除浮动 的多种方式(clearfix)
			
说明 本文适合知道HTML 与 CSS基础知识的读者,或者想要了解清除浮动背后原理的读者! 1.什么是浮动 首先我们需要知道定位 元素在页面中的位置就是定位,解决问题之前我们先来了解下几种定位方式 : ...
 - TODO monkey笔记,PC端执行和手机端执行
			
微博不给力啊 吞我笔记,还好我有txt... 1.环境准备:安装Android sdk, 配置环境变量:platfrom_tools,tools,aapt;java:2.查询当前apk信息: aapt ...