package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: SortArrayByParity
* @Author: xiaof
* @Description: 905. Sort Array By Parity
* 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.
*
* 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.
*
* @Date: 2019/7/3 8:48
* @Version: 1.0
*/
public class SortArrayByParity { public int[] solution(int[] A) {
//先获取所有偶元素,然后获取所有奇元元素,还有一个方法就是交换前后位置
//吧所有偶数放到前面,奇数放后面,类似快排
int index1 = 0, index2 = A.length - 1;
while(index1 < index2) {
//遍历起始第一个不是偶数的位置
while(index1 < A.length && (A[index1] & 1) == 0) {
index1++;
}
//后面往前,找到第一个不是奇数
while(index2 > 0 && (A[index2] & 1) == 1) {
index2--;
}
//交换位置
if(index1 < index2 && index1 < A.length && index2 > 0) {
//交换位置
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
} return A;
} public static void main(String args[]) {
int A[] = {3,1,2,4};
SortArrayByParity fuc = new SortArrayByParity();
System.out.println(fuc.solution(A));
}
}

【LEETCODE】41、905. Sort Array By Parity的更多相关文章

  1. 【LEETCODE】42、922. Sort Array By Parity II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  2. 【Leetcode_easy】905. Sort Array By Parity

    problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  6. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  7. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  8. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  9. 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 ...

随机推荐

  1. 使用vault pki 为nginx 生成tls 证书文件

    关于vault pki 管理的使用的可以参考官方文档或者docker-vault 以下演示一个简单的基于vault pki 为nginx 提供tls 证书 项目环境配置 nginx 配置文件   wo ...

  2. hdfs、yarn集成ranger

    一.安装hdfs插件 从源码安装ranger的服务器上拷贝hdfs的插件到你需要安装的地方 1.解压安装 # tar zxvf ranger-2.1.0-hdfs-plugin.tar.gz -C / ...

  3. Tcl数学运算

    expr 数学表达式 Tcl支持的数学操作符(优先级按照从高到低): -一元负号 +一元正号 ~按位取反 !逻辑非 *乘 /除 %取余 +加号 -减号 <<左移位 >>右移位 ...

  4. px,em和rem

    1 px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的 2 em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字 ...

  5. 2019软工实践_Alpha(5/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11898112.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  6. WEB API 的设计与开发

  7. Mac使用秘钥登录Linux服务器

    简介 在 Mac 上配置 SSH 密钥登录远程的 Linux 相关配置 1.创建本地的 SSH 密钥 本地 生成秘钥对 ssh-keygen -t rsa -C 'youxiang@aliyun.co ...

  8. XmlIgnore的解释和使用

    XmlIgnore是一个自定义属性,用来指明在序列化时是否序列化一个属性.如下面的例子: public class Group { public string GroupName; [XmlIgnor ...

  9. mqtt 与 MQ 的区别

    mqtt 与 MQ 的区别: mqtt:一种通信协议,类似人类交谈中的汉语.英语.俄语中的一种语言规范MQ:一种通信通道,也叫消息队列,类似人类交谈中的用电话.email.微信的一种通信方式json: ...

  10. Oracle定时任务执行存储过程备份日志记录表

    写在前面 需求 1.备份系统日志表T_S_LOG, 按照操作时间字段OPERATETIME, 将每天的日志增量备份到另一张表. 思路 1.创建一张数据结构完全相同的表T_S_LOG_BAK作为备份表 ...