Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 11, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions problems/0922.按奇偶排序数组II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

[力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/)

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
给定一个非负整数数组 nums, nums 中一半整数是奇数,一半整数是偶数。

对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
对数组进行排序,以便当 nums[i] 为奇数时,i 也是奇数;当 nums[i] 为偶数时, i 也是偶数。

你可以返回任何满足上述条件的数组作为答案。

Expand All @@ -35,17 +35,17 @@
```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());
vector<int> sortArrayByParityII(vector<int>& nums) {
vector<int> even(nums.size() / 2); // 初始化就确定数组大小,节省开销
vector<int> odd(nums.size() / 2);
vector<int> result(nums.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];
// 把nums数组放进偶数数组,和奇数数组
for (int i = 0; i < nums.size(); i++) {
if (nums[i] % 2 == 0) even[evenIndex++] = nums[i];
else odd[oddIndex++] = nums[i];
}
// 把偶数数组,奇数数组分别放进result数组中
for (int i = 0; i < evenIndex; i++) {
Expand All @@ -62,22 +62,22 @@ public:

### 方法二

以上代码我是建了两个辅助数组,而且A数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助树,代码如下:
以上代码我是建了两个辅助数组,而且nums数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助数组,代码如下:

```CPP
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> result(A.size());
vector<int> sortArrayByParityII(vector<int>& nums) {
vector<int> result(nums.size());
int evenIndex = 0; // 偶数下标
int oddIndex = 1; // 奇数下标
for (int i = 0; i < A.size(); i++) {
if (A[i] % 2 == 0) {
result[evenIndex] = A[i];
for (int i = 0; i < nums.size(); i++) {
if (nums[i] % 2 == 0) {
result[evenIndex] = nums[i];
evenIndex += 2;
}
else {
result[oddIndex] = A[i];
result[oddIndex] = nums[i];
oddIndex += 2;
}
}
Expand All @@ -96,15 +96,15 @@ public:
```CPP
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> sortArrayByParityII(vector<int>& nums) {
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]); // 替换
for (int i = 0; i < nums.size(); i += 2) {
if (nums[i] % 2 == 1) { // 在偶数位遇到了奇数
while(nums[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数
swap(nums[i], nums[oddIndex]); // 替换
}
}
return A;
return nums;
}
};
```
Expand Down Expand Up @@ -253,6 +253,37 @@ func sortArrayByParityII(nums []int) []int {
}
return result;
}

// 方法二
func sortArrayByParityII(nums []int) []int {
result := make([]int, len(nums))
evenIndex := 0 // 偶数下标
oddIndex := 1 // 奇数下标
for _, v := range nums {
if v % 2 == 0 {
result[evenIndex] = v
evenIndex += 2
} else {
result[oddIndex] = v
oddIndex += 2
}
}
return result
}

// 方法三
func sortArrayByParityII(nums []int) []int {
oddIndex := 1
for i := 0; i < len(nums); i += 2 {
if nums[i] % 2 == 1 { // 在偶数位遇到了奇数
for nums[oddIndex] % 2 != 0 {
oddIndex += 2 // 在奇数位找一个偶数
}
nums[i], nums[oddIndex] = nums[oddIndex], nums[i]
}
}
return nums
}
```

### JavaScript
Expand Down

AltStyle によって変換されたページ (->オリジナル) /