原文链接: https://leetcode-cn.com/problems/sort-array-by-parity-ii
英文原文
Given an array of integers nums
, half of the integers in nums
are odd, and the other half are even.
Sort the array so that whenever nums[i]
is odd, i
is odd, and whenever nums[i]
is even, i
is even.
Return any answer array that satisfies this condition.
Example 1:
Input: nums = [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.
Example 2:
Input: nums = [2,3] Output: [2,3]
Constraints:
2 <= nums.length <= 2 * 104
nums.length
is even.- Half of the integers in
nums
are even. 0 <= nums[i] <= 1000
Follow Up: Could you solve it in-place?
中文题目
给定一个非负整数数组 A
, A 中一半整数是奇数,一半整数是偶数。
对数组进行排序,以便当 A[i]
为奇数时,i
也是奇数;当 A[i]
为偶数时, i
也是偶数。
你可以返回任何满足上述条件的数组作为答案。
示例:
输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。
提示:
2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
通过代码
高赞题解
方法一:两次遍历
思路和算法
遍历一遍数组把所有的偶数放进 $\textit{ans}[0]$,$\textit{ans}[2]$,$\textit{ans}[4]$,以此类推。
再遍历一遍数组把所有的奇数依次放进 $\textit{ans}[1]$,$\textit{ans}[3]$,$\textit{ans}[5]$,以此类推。
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
int i = 0;
for (int x: nums) {
if (x % 2 == 0) {
ans[i] = x;
i += 2;
}
}
i = 1;
for (int x: nums) {
if (x % 2 == 1) {
ans[i] = x;
i += 2;
}
}
return ans;
}
};
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int i = 0;
for (int x : nums) {
if (x % 2 == 0) {
ans[i] = x;
i += 2;
}
}
i = 1;
for (int x : nums) {
if (x % 2 == 1) {
ans[i] = x;
i += 2;
}
}
return ans;
}
}
int* sortArrayByParityII(int* nums, int numsSize, int* returnSize) {
int* ans = malloc(sizeof(int) * numsSize);
int add = 0;
for (int i = 0; i < numsSize; i++) {
if (nums[i] % 2 == 0) {
ans[add] = nums[i];
add += 2;
}
}
add = 1;
for (int i = 0; i < numsSize; i++) {
if (nums[i] % 2 == 1) {
ans[add] = nums[i];
add += 2;
}
}
*returnSize = numsSize;
return ans;
}
var sortArrayByParityII = function(nums) {
const n = nums.length;
const ans = new Array(n);
let i = 0;
for (const x of nums) {
if (!(x & 1)) {
ans[i] = x;
i += 2;
}
}
i = 1;
for (const x of nums) {
if (x & 1) {
ans[i] = x;
i += 2;
}
}
return ans;
};
func sortArrayByParityII(nums []int) []int {
ans := make([]int, len(nums))
i := 0
for _, v := range nums {
if v%2 == 0 {
ans[i] = v
i += 2
}
}
i = 1
for _, v := range nums {
if v%2 == 1 {
ans[i] = v
i += 2
}
}
return ans
}
复杂度分析
时间复杂度:$O(N)$,其中 $N$ 是数组 $\textit{nums}$ 的长度。
空间复杂度:$O(1)$。注意在这里我们不考虑输出数组的空间占用。
方法二:双指针
思路与算法
如果原数组可以修改,则可以使用就地算法求解。
为数组的偶数下标部分和奇数下标部分分别维护指针 $i, j$。随后,在每一步中,如果 $\textit{nums}[i]$ 为奇数,则不断地向前移动 $j$(每次移动两个单位),直到遇见下一个偶数。此时,可以直接将 $\textit{nums}[i]$ 与 $\textit{nums}[j]$ 交换。我们不断进行这样的过程,最终能够将所有的整数放在正确的位置上。
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
int n = nums.size();
int j = 1;
for (int i = 0; i < n; i += 2) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
swap(nums[i], nums[j]);
}
}
return nums;
}
};
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int n = nums.length;
int j = 1;
for (int i = 0; i < n; i += 2) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
swap(nums, i, j);
}
}
return nums;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
void swap(int* a, int* b) {
int t = *a;
*a = *b, *b = t;
}
int* sortArrayByParityII(int* nums, int numsSize, int* returnSize) {
int j = 1;
for (int i = 0; i < numsSize; i += 2) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
swap(nums + i, nums + j);
}
}
*returnSize = numsSize;
return nums;
}
const swap = (nums, i, j) => {
const temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
};
var sortArrayByParityII = function(nums) {
const n = nums.length;
let j = 1;
for (let i = 0; i < n; i += 2) {
if (nums[i] & 1) {
while (nums[j] & 1) {
j += 2;
}
swap(nums, i, j);
}
}
return nums;
};
func sortArrayByParityII(nums []int) []int {
for i, j := 0, 1; i < len(nums); i += 2 {
if nums[i]%2 == 1 {
for nums[j]%2 == 1 {
j += 2
}
nums[i], nums[j] = nums[j], nums[i]
}
}
return nums
}
复杂度分析
时间复杂度:$O(N)$,其中 $N$ 是数组 $\textit{nums}$ 的长度。
空间复杂度:$O(1)$。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
91535 | 128218 | 71.4% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|