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 integers are even.
Sort the array so that whenever A[i]
is odd, i
is odd; and whenever A[i]
is even, i
is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
Idea 1. Similar to Sort Array By Parity LT905, assume the array is in the order, what to do with next element?
Time complexity: O(n)
Space complexity: O(1)
class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; even< A.length; even += 2) {
if(A[even]%2 == 1) {
while(odd < A.length && A[odd]%2 == 1) {
odd += 2;
}
swap(A, even, odd);
}
} return A;
}
}
class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; even < A.length; even +=2) {
if((A[even]&1) == 1) {
while((A[odd]&1) == 1) {
odd += 2;
}
swap(A, even, odd);
}
} return A;
}
}
Idea 1.a two pointers walking towards each other
class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; odd < A.length && even < A.length;) {
if(A[even]%2 == 1&& A[odd]%2 == 0) {
swap(A, even, odd);
}
if(A[even]%2 == 0) {
even +=2;
}
if(A[odd]%2 == 1) {
odd += 2;
}
} return A;
}
}
use a&1 == 1 instead of a%2 == 1 to check parity
class Solution {
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public int[] sortArrayByParityII(int[] A) {
for(int even = 0, odd = 1; odd < A.length && even < A.length; ) {
if((A[even]&1) == 1 && (A[odd]&1) == 0) {
swap(A, even, odd);
}
if((A[even]&1) == 0) {
even += 2;
}
if((A[odd]&1) == 1) {
odd += 2;
}
} return A;
}
}
Sort Array By Parity II LT922的更多相关文章
- 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 ...
- 992. Sort Array By Parity II - LeetCode
Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...
- [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 ...
- [Swift]LeetCode922.按奇偶排序数组 II | 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 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. Sort Array By Parity II
https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...
- leetcode922 Sort Array By Parity II
""" Given an array A of non-negative integers, half of the integers in A are odd, and ...
随机推荐
- ionic3安卓平台引用高德地图
1.前置条件 第一步,注册高德开发者:第二步,去控制台创建应用:第三步,获取Key. 2.打开src目录下的index.html, 在head标签中添加以下代码,引入js: <script ty ...
- tinycc update VERSION to 0.9.27
TinyCC全称为Tiny C Compiler, 是微型c编译器,可在linux/win/平台上编译使用. 在用代码里面使用tcc当脚本,性能比lua还快,目前已有网游服务端使用TCC脚本提高性能. ...
- C#编程经验-VS Debug
F11 OneStepDebugF10 ProcessDebugbreakPointDebug(quick location,then use one step debug)
- 【OpenStack】相关概念
网络 network和subnet Service subnets: 创建network,subnet, instances 官方示例 Network components: Switches/ Ro ...
- Pod配置PersistentVolumeClaim详解
1,创建PersistentVolume kind: PersistentVolume apiVersion: v1 metadata: name: task-pv-volume labels: ty ...
- PP.io的三个阶段,“强中心”——“弱中心”——“去中心”
什么是PP.io? PP.io是我和Bill发起的存储项目,目的在于为开发者提供一个去中心化的存储和分发平台,能做到更便宜,更高速,更隐私. 当然做去中心化存储的项目也有好几个,FileCoin,Si ...
- NAP(Network Access Protection)
- day28元类与异常查找
元类与异常处理1. 什么是异常处理 异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常 没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止 异常包含三个部分: ...
- RocketMQ入门(Filter)_5
RocketMQ中存储的消息对于消费者来说,并不完全都是他们需要的,因此需要对消息进行过滤. 订阅Topic主题 ,选择Tags都是我们简单的过滤.Topic是大分类,Tags是二级分类. Rocke ...
- TCC细读 - 1 例子流程
http://www.iocoder.cn/categories/TCC-Transaction/ https://github.com/changmingxie/tcc-transaction 细读 ...