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. WinDbg扩展

    WinDbg的扩展,也可以叫插件.它用于实现针对特定调试目标的调试功能,用于扩展某一方面的调试功能.扩展的实现是通过扩展模块(DLL)实现的.Windbg本身已经包含了很多扩展命令,这些扩展为这Win ...

  2. pgloader 学习(八) pg 2 pg 简单demo

    pg 数据到pg 数据的迁移,同时支持名称的变更 环境准备 docker-compose文件 内容偏多可以忽略部分 version: "3" services: pgloader- ...

  3. let

    let a=2+2 #+ - * / % ** 都支持 支持类C的计算方式 let i++ let i-- let i+=10 let i-=10 let i*=10 let i/=10 let i% ...

  4. P3746 【[六省联考2017]组合数问题】

    题目是要我们求出如下柿子: \[\sum_{i=0}^{n}C_{nk}^{ik+r}\] 考虑k和r非常小,我们能不能从这里切入呢? 如果你注意到,所有组合数上方的数\(\%k==r\),那么是不是 ...

  5. CSS — BEM 命名规范

    推荐阅读: https://juejin.im/post/5b925e616fb9a05cdd2ce70d 1 什么是 BEM 命名规范 Bem 是块(block).元素(element).修饰符(m ...

  6. 服务器收不到支付宝notify_url异步回调请求的问题排查

    小背景 最近在调整支付宝支付的功能时发现,不能够正常接收支付宝付款成功之后的回调通知了,从代码到配置最后到服务器配置都排查了一遍,最终发现问题原因竟然是因为我们的回调地址notify_url是http ...

  7. ubuntu之路——day14 只用python的numpy在底层实现多层神经网络

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...

  8. better-scroll在vue项目中的使用

    1.准备工作 在项目中安装better-scroll: npm install --save better-scroll 组件中引入插件 import BScroll from "bette ...

  9. m4a 转MP3

    import os for filename in os.listdir(r'.'): print filename os.rename(filename,filename.replace(' ',' ...

  10. CentOS 6和 CentOS 7的区别【转】

    虽然,redhat 8在今年已经推出了,但是centos 8还没有推出.而且公司好多都在用centos 6和7 来了解一下6和7的区别吧 整体说明 1.系统 项目CentOS 6CentOS7 . 安 ...