leetcode-master/problems/0922.按奇偶排序数组II.md

171 lines
4.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p align="center">
<a href="https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ"><img src="https://img.shields.io/badge/PDF下载-代码随想录-blueviolet" alt=""></a>
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a>
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
</p>
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 922. 按奇偶排序数组II
给定一个非负整数数组 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] 也会被接受。
# 思路
这道题目直接的想法可能是两层for循环再加上used数组表示使用过的元素。这样的的时间复杂度是O(n^2)。
## 方法一
其实这道题可以用很朴实的方法时间复杂度就就是O(n)了C++代码如下:
```CPP
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> even(A.size() / 2); // 初始化就确定数组大小,节省开销
vector<int> odd(A.size() / 2);
vector<int> result(A.size());
int evenIndex = 0;
int oddIndex = 0;
int resultIndex = 0;
// 把A数组放进偶数数组和奇数数组
for (int i = 0; i < A.size(); i++) {
if (A[i] % 2 == 0) even[evenIndex++] = A[i];
else odd[oddIndex++] = A[i];
}
// 把偶数数组奇数数组分别放进result数组中
for (int i = 0; i < evenIndex; i++) {
result[resultIndex++] = even[i];
result[resultIndex++] = odd[i];
}
return result;
}
};
```
时间复杂度O(n)
空间复杂度O(n)
## 方法二
以上代码我是建了两个辅助数组而且A数组还相当于遍历了两次用辅助数组的好处就是思路清晰优化一下就是不用这两个辅助树代码如下
```CPP
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> result(A.size());
int evenIndex = 0; // 偶数下表
int oddIndex = 1; // 奇数下表
for (int i = 0; i < A.size(); i++) {
if (A[i] % 2 == 0) {
result[evenIndex] = A[i];
evenIndex += 2;
}
else {
result[oddIndex] = A[i];
oddIndex += 2;
}
}
return result;
}
};
```
时间复杂度O(n)
空间复杂度O(n)
## 方法三
当然还可以在原数组上修改连result数组都不用了。
```CPP
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
int oddIndex = 1;
for (int i = 0; i < A.size(); i += 2) {
if (A[i] % 2 == 1) { // 在偶数位遇到了奇数
while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数
swap(A[i], A[oddIndex]); // 替换
}
}
return A;
}
};
```
时间复杂度O(n)
空间复杂度O(1)
这里时间复杂度并不是O(n^2)因为偶数位和奇数位都只操作一次不是n/2 * n/2的关系而是n/2 + n/2的关系
# 其他语言版本
## Java
```java
// 方法一
class Solution {
public int[] sortArrayByParityII(int[] nums) {
// 分别存放 nums 中的奇数、偶数
int len = nums.length;
int evenIndex = 0;
int oddIndex = 0;
int[] even = new int[len / 2];
int[] odd = new int[len / 2];
for (int i = 0; i < len; i++) {
if (nums[i] % 2 == 0) {
even[evenIndex++] = nums[i];
} else {
odd[oddIndex++] = nums[i];
}
}
// 把奇偶数组重新存回 nums
int index = 0;
for (int i = 0; i < even.length; i++) {
nums[index++] = even[i];
nums[index++] = odd[i];
}
return nums;
}
}
```
## Python
```python
```
## Go
```go
```
## JavaScript
```js
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>