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 ...
随机推荐
- pyhton3.5将汉字转成二进制的方法
直接上代码:name = "你好,中国人"byteName = bytes(name.encode("utf-8"))print(byteName)for b ...
- 斐讯自动下单抢购V1.3.4【自动验证码识别】
20180530 更新 V1.3.41.增加有货下单:替代定时下单 20180519 更新 V1.3.31.增加订单满减优惠:支付宝每单立减5元2.修改商城域名及下单速度 功能介绍1.斐讯商城抢购专用 ...
- 当通过Nuget包管理器获取还原组时,出现 提示 “xxxxx”已拥有为“xxxxx”定义的依赖项
当通过Nuget包管理器获取还原组件时,出现 提示 “xxxxx”已拥有为“xxxxx”定义的依赖项 时 解决方法: 工具---扩展和更新,把Nuget包管理器卸载后,重启VS,再安装,现打开VS项 ...
- PUSU 拆分后发货和开票的时间节点问题
项目做到现在业务突然说流程要变,心中顿时无数个草草草掠过.这公司业务也真是够奇葩了,一天一个样.原来流程是由PU把产品生产完后就发给SU,由SU再来决定什么时候对客户和开票.而现在马上要上线了,突然冒 ...
- CSV文件乱码展示(编码格式问题)
最开始mac上打开CSV文件乱码,是这样的:CSV文件编码格式为UTF-8 解决办法一:将excel文件同样的转换编码格式为utf-8,具体操作如下: 去掉tab,勾选comma 最后,将文件另存为u ...
- Python中字符串/字典/json之间的转换
import json #定义一个字典d1,字典是无序的 d1 = { "a": None, "b": False, "c": True, ...
- 深入学习 Java 序列化
前言 对于Java的序列化,一直只知道只需要实现Serializbale这个接口就可以了,具体内部实现一直不是很了解,正好这次在重复造RPC的轮子的时候涉及到序列化问题,就抽时间看了下 Java序列化 ...
- android 开发 更好的数据存放,回调,处理class模式,适合与各种布局适配器class一起使用
预先导入数据模式: /** * Created by lenovo on 2018/5/18. */ public class DeivceListData { private DeivceListD ...
- 分布式计算课程补充笔记 part 3
▶ OpenMP 的任务并行 (task parallelism):显式定义一系列可执行的任务及其相互依赖关系,通过任务调度的方式多线程动态执行,支持任务的延迟执行 (deferred executi ...
- Linux下编译安装FFmpeg
FFmpeg官网:http://www.ffmpeg.org 官网介绍 FFmpeg is the leading multimedia framework, able to decode, enco ...