题目

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.

Note:

  • 1 <= A.length <= 5000
  • 0 <= A[i] <= 5000

题解

题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序

思路:建两个list,一个放偶数,一个放奇数,最后将两个list合并,转化为数组返回

class Solution {
public int[] sortArrayByParity(int[] A) {
ArrayList<Integer> oddList = new ArrayList<>();//存放奇数
ArrayList<Integer> evenList = new ArrayList<>();//存放偶数
for (int a : A) {
if (a % 2 == 0) evenList.add(a);
else oddList.add(a);
}
evenList.addAll(oddList);
int[] arr = new int[A.length];
for (int i = 0; i < A.length; i ++){
arr[i] = evenList.get(i);
}
return arr;
}
}

LeetCode 905. Sort Array By Parity 按奇偶校验排列数组的更多相关文章

  1. [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, ...

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

  3. LeetCode 905 Sort Array By Parity 解题报告

    题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...

  4. [leetcode] 905. Sort Array By Parity [easy]

    原题链接 很水的一道题,就是数组内部交换. 水题就想着减少复杂度嘛,于是学到一种交换写法. class Solution { public: vector<int> sortArrayBy ...

  5. 【LEETCODE】41、905. Sort Array By Parity

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

  6. 905. Sort Array By Parity - LeetCode

    Question 905. Sort Array By Parity Solution 题目大意:数组排序,偶数放前,奇数在后,偶数的数之间不用管顺序,奇数的数之间也不用管顺序 思路:建两个list, ...

  7. 【Leetcode_easy】905. Sort Array By Parity

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

  8. [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 ...

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

随机推荐

  1. 浅谈Java中switch分支语句

    前言: 在程序中遇到多分支选择的时候,想必大家都喜欢用if...else if...else...语句,尤其是初学者,因为在了解switch语句之前,我也是只会用if...else语句.那么现在看完这 ...

  2. 4-1-JS数据类型及相关操作

    js的数据类型 判断数据类型 用typeof   typeof "John"                 // alert(typeof "John") 返 ...

  3. 登陆远程服务器的Tomcat 8的manger的页面403

    访问远程服务器Tomcat 8的管理页面报错 在远程服务器上安装了一个tomcat8.5,配置好用户后重新启动tomcat ,发现:8080可以访问,但是进入:8080/manager/html页面报 ...

  4. Mysql—配置文件my.ini或my.cnf的详解

    [mysqld] log_bin = mysql-bin binlog_format = mixed expire_logs_days = # 超过7天的binlog删除 slow_query_log ...

  5. 用户和组的权限管理/特殊权限和ACL(个人笔记)

    个人学习笔记总结 内容包括解释Linux的安全模型解释用户帐号和组群帐号的目的用户和组管理命令理解并设置文件权限默认权限特殊权限ACL介绍安全3A资源分派:Authentication:认证Auth ...

  6. 29.Java基础_接口

    接口的成员特点

  7. vue项目关闭eslint校验

    [前言] eslint是一个JavaScript的校验插件,通常用来校验语法或代码的书写风格.这篇文章主要介绍了vue项目关闭eslint校验,需要的朋友可以参考下 [主体] 简介eslint esl ...

  8. jacoco统计自动化代码覆盖率

    jacoco统计自动化代码覆盖率 1. 简介 1.1. 什么是Jacoco Jacoco是一个开源的代码覆盖率工具,可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以 ...

  9. windows server2008 R2下mysql 5.7版本中修改编码为utf-8的方法步骤

    首先通过 show variables like 'character_set_%'; 查看mysql字符集情 默认编码为 latin1 然后关闭数据库 在mysql安装目录下找到my.ini文件 在 ...

  10. JWT签名算法中HS256和RS256有什么区别 转载

    JWT签名算法中,一般有两个选择,一个采用HS256,另外一个就是采用RS256. 签名实际上是一个加密的过程,生成一段标识(也是JWT的一部分)作为接收方验证信息是否被篡改的依据. RS256 (采 ...