题目

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对象+GC

    第五天了.. 理清了Java对象的创建过程,分配内存,线程安全性,对象头和对象的访问定位 理清了JVM GC的发展历史,算法,例如: 可达性分析 引用计数法 标记-清楚法 复制算法 标记-整理算法 分 ...

  2. [PHP] 配置vscode的语法检测消除提示Cannot validate since no PHP executable is set

    默认下载完vscode什么都不做,会提示一下信息Cannot validate since no PHP executable is set. Use the setting 'php.validat ...

  3. nginx搭建代理服务器与负载均衡器

    一.代理服务器 服务 功能 配置语法 默认 配置位置 配置举例 结果验证 备注 代理服务 反向代理 proxy_pass URL   location.if in location.limit_exc ...

  4. C# 深入分析 GC 处理机制

    引用地址:https://www.cnblogs.com/nele/p/5673215.html GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年 ...

  5. 日志分析利器Splunk的搭建、使用、破解

    博主对splunk的了解不多,博主的使用目的是为了同步,分析日志.当初的搭建也是为了公司申请牌照需要日志服务器分析日志,顺便自己也对这方面感兴趣就买了本书看了看搭建玩的,后来检查来了博主也给他们演示了 ...

  6. Tensorflow之多元线性回归问题(以波士顿房价预测为例)

    一.根据波士顿房价信息进行预测,多元线性回归+特征数据归一化 #读取数据 %matplotlib notebook import tensorflow as tf import matplotlib. ...

  7. appium常使用的命令

    1.查看apk安装包的appPackagehe和appActivity aapt dump badging E:\taobao.apk > E:\taobao.txt   -- 将appPack ...

  8. 【转】Redis常见面试题

    介绍:Redis 是一个开源的使用 ANSI C 语言编写.遵守 BSD 协议.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的 API的非关系型数据库. 传统数据 ...

  9. JDOJ3011 铺地板III

    JDOJ3011 铺地板III https://neooj.com/oldoj/problem.php?id=3011 题目描述 有3 x N (0 <= N <= 105)的网格,需要用 ...

  10. UVA11374 Airport Express 正反两次最短路

    问题描述 洛谷(有翻译) 吐槽 一道坑题. 如何对待商务票 因为商务票只有一张,所以在\(k\)条边中只有一条边会被选中,很显然,最后这条边会被枚举. 如何选择使用商务票的边 假设我们正在枚举这条边, ...