Sort Array By Parity LT905
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
Idea 1. Borrow the partition idea from quicksort, like Dutch flag problem, assume the array is already sorted as required, what to do with new element?
even | odd | ?
Time complexity: O(n)
Space complexity: O(1)
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
} public int[] sortArrayByParity(int[] A) {
int evenEnd = -1;
for(int i = 0; i < A.length; ++i) {
if(A[i]%2 == 0) {
++evenEnd;
swap(A, evenEnd, i);
}
}
return A;
}
}
Idea 1.a Two pointers walking toward each other and swap if not in the right place
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
} public int[] sortArrayByParity(int[] A) {
for(int left = 0, right = A.length-1; left < right; ) {
while(left < right && A[left]%2 == 0) {
++left;
} while(left < right && A[right]%2 == 1) {
--right;
} if(left < right) {
swap(A, left, right);
++left;
--right;
}
}
return A;
}
}
slightly more cleaner
class Solution {
private void swap(int[] A, int i, int j) {
int a = A[i];
A[i] = A[j];
A[j] = a;
} public int[] sortArrayByParity(int[] A) {
for(int left = 0, right = A.length-1; left < right; ) {
if(A[left]%2 == 1 && A[right]%2 == 0) {
swap(A, left, right);
} if(A[left]%2 == 0) {
++left;
} if(A[right]%2 == 1) {
--right;
}
}
return A;
}
}
Idea 2. customised comparator to sort
Time complexity: O(nlogn)
Space complexity: O(n)
class Solution {
public int[] sortArrayByParity(int[] A) { Integer[] B = new Integer[A.length];
for(int i = 0; i < A.length; ++i) {
B[i] = A[i];
} Comparator<Integer> cmp = (a, b) -> Integer.compare(a%2, b%2);
Arrays.sort(B, cmp); for(int i = 0; i < A.length; ++i) {
A[i] = B[i];
} return A;
}
}
Sort Array By Parity LT905的更多相关文章
- 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 C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- 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 ...
- [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 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】905. Sort Array By Parity
problem 905. Sort Array By Parity solution1: class Solution { public: vector<int> sortArrayByP ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
随机推荐
- Java判断一个字符串中有多少大写字母、小写字母和数字
Java判断一个字符串中有多少大写字母.小写字母和数字 思路: 大写字母就是A-Z之间,小写字母是a-z之间,数字就是0-9之间,于是做判断就好:用到的String知识点,遍历字符串, 长度方法len ...
- OnContextMenu实现禁止鼠标右键
OnContextMenu事件 定义和使用:oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单.注意:所有浏览器都支持 oncontextmenu 事件, contextmen ...
- django admin后台设置
#encoding:utf-8 from django.contrib import admin from son10.models import * # Register your models h ...
- spark快速开发之scala基础之1 数据类型与容器
写在前面 面向java开发者.实际上,具有java基础学习scala是很容易.java也可以开发spark,并不比scala开发的spark程序慢.但学习scala可有助于更快更好的理解spark.比 ...
- 【pyspider】启动爬虫后在results页面没有看到结果
今天根据书上的介绍写了一个简单爬虫,爬取豌豆荚里面APP的基本信息,但是在调试结果正常后,发现跳转到result页面后没有看到结果. 后来上网查了一下,发现要在def detail_page(self ...
- unity实现3D物体上的事件监听处理
想要在3D物体上实现全套事件监听处理: OnMouse系列 OnTrigger系列 OnPointer系列 OnDrag系列 1.在相机中添加Physics Raycaster组件 2.3D物体上 ...
- const和volatile分析
c语言中const修饰的变量是只读的,不能直接作为赋值号的左值,其本质还是变量:会占用内存空间:本质上const在编译器有用,运行时无用(还是可以通过指针改变它的值) ; int *p=&ab ...
- VCSA 6.5, 初始化设置root密码失败can't set root password 或 安装时卡在80%
是因为下载的非官方的包密码过期了,如果是Windows引导安装: 安装完虚拟机的时候就马上执行下面步骤,修改密码有效期即可. 重启vcsa,在引导界面(photon的图形界面)里按e编辑启动项 在co ...
- Topological Sorting拓扑排序
定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...
- android 下拉刷新框架PullToRefreshScrollView(com.handmark.pulltorefresh)
很简单,实现OnRefreshListener这个监听器. mPullRefreshScrollView .setOnRefreshListener(new OnRefreshListener< ...