992. Sort Array By Parity II - LeetCode
Question
Solution
题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数位上
思路:把奇数数和偶数数筛选出来后对其分别排序,再按奇偶索引放到原数组上
Java实现:
public int[] sortArrayByParityII(int[] A) {
List<Integer> oddList = new ArrayList<>();
List<Integer> evenList = new ArrayList<>();
for (int a : A) {
if (a % 2 == 0) evenList.add(a);
else oddList.add(a);
}
Collections.sort(oddList);
Collections.sort(evenList);
for (int i = 0; i < oddList.size(); i++) {
A[2 * i] = evenList.get(i);
A[2 * i + 1] = oddList.get(i);
}
return A;
}
992. Sort Array By Parity II - LeetCode的更多相关文章
- 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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- [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 ...
- Sort Array By Parity II LT922
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- #Leetcode# 922. Sort Array By Parity II
https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 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 i ...
- LeetCode.922-按奇偶排序数组 II(Sort Array By Parity II)
这是悦乐书的第354次更新,第379篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第216题(顺位题号是922).给定非负整数的数组A,A中的一半整数是奇数,而剩下的一半 ...
随机推荐
- 分享一个自己写的基于canvas的原生js图片爆炸插件
DEMO访问地址: https://bupt-hjm.github.io/BoomGo/博客地址: http://bupt-hjm.github.io/2016/07/10/boom/插件及使用方法地 ...
- es6-Set与Map
se5中的set与map 在est5中开发者使用对象属性来模拟.set多用于检查键的存在,map多用于提取数据. { let set = Object.create(null) set.foo = t ...
- 关于recyclerview其item数据重复问题
查找方法(query)的list只定义对象,不实例化,等到要添加的时候,再new一个新的对象出来. 千万不要如下图这样,否则item显示出来的永远是最新数据. (这个bug找了两天,还是基本功不扎实, ...
- AS之去掉顶部标题栏
在该目录下,将原本<style name的这行代码改为: <style name="Theme.Tongxunlu" parent="Theme.Materi ...
- Hadoop本地编写的jar包放到集群执行时报错处理
错误描述: 020-03-24 22:45:23,204 WARN org.apache.hadoop.yarn.server.nodemanager.DefaultContainerExecutor ...
- idea启动tomcat后控制台日志显示中文乱码问题
想必有些人 会遇到 控制台中文乱码: 可以通过以下方法解决该中文乱码问题: 1. 点击Help => Edit custom VM Options,在最后面添加 "-Dfile.en ...
- python的蟒蛇绘制
代码: #PythonDraw.py import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-250) turtle ...
- 【论文阅读】ICLR 2022: Scene Transformer: A unified architecture for predicting future trajectories of multiple agents
ICLR 2022: Scene Transformer: A unified architecture for predicting future trajectories of multiple ...
- springboot+mybatis实现数据分页(三种方式)
项目准备 1.创建用户表 2.使用spring初始化向导快速创建项目,勾选mybatis,web,jdbc,driver 添加lombok插件 <?xml version="1.0&q ...
- Hash冲突以及解决
哈希函数:它把一个大范围的数字哈希(转化)成一个小范围的数字,这个小范围的数对应着数组的下标.使用哈希函数向数组插入数据后,这个数组就是哈希表. 冲突 当冲突产生时,一个方法是通过系统的方法找到数组的 ...