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

Commit b13b1d1

Browse files
feat: add solutions to lc problem: No.0922 (#4019)
No.0922.Sort Array By Parity II
1 parent bbf6fec commit b13b1d1

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

‎solution/0900-0999/0922.Sort Array By Parity II/README.md‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ tags:
6464

6565
### 方法一:双指针
6666

67-
我们用两个指针 $i$ 和 $j$ 分别指向偶数下标和奇数下标。
67+
我们用两个指针 $i$ 和 $j$ 分别指向偶数下标和奇数下标,初始时 $i = 0,ドル $j = 1$
6868

69-
当 $i$ 指向偶数下标时,如果 $nums[i]$ 是奇数,那么我们需要找到一个奇数下标 $j,ドル使得 $nums[j]$ 是偶数,然后交换 $nums[i]$ 和 $nums[j]$。继续遍历,直到 $i$ 指向数组末尾。
69+
当 $i$ 指向偶数下标时,如果 $\textit{nums}[i]$ 是奇数,那么我们需要找到一个奇数下标 $j,ドル使得 $\textit{nums}[j]$ 是偶数,然后交换 $\textit{nums}[i]$ 和 $\textit{nums}[j]$。继续遍历,直到 $i$ 指向数组末尾。
7070

71-
时间复杂度 $O(n),ドル其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
71+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $\textit{nums}[i]$ 的长度。空间复杂度 $O(1)$。
7272

7373
<!-- tabs:start -->
7474

@@ -157,6 +157,26 @@ function sortArrayByParityII(nums: number[]): number[] {
157157
}
158158
```
159159

160+
#### Rust
161+
162+
```rust
163+
impl Solution {
164+
pub fn sort_array_by_parity_ii(mut nums: Vec<i32>) -> Vec<i32> {
165+
let n = nums.len();
166+
let mut j = 1;
167+
for i in (0..n).step_by(2) {
168+
if nums[i] % 2 != 0 {
169+
while nums[j] % 2 != 0 {
170+
j += 2;
171+
}
172+
nums.swap(i, j);
173+
}
174+
}
175+
nums
176+
}
177+
}
178+
```
179+
160180
#### JavaScript
161181

162182
```js

‎solution/0900-0999/0922.Sort Array By Parity II/README_EN.md‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ tags:
6161

6262
### Solution 1: Two Pointers
6363

64-
We use two pointers $i$ and $j$ to point to even and odd indices respectively.
64+
We use two pointers $i$ and $j$ to point to even and odd indices, respectively. Initially, $i = 0$ and $j = 1$.
6565

66-
When $i$ points to an even index, if $nums[i]$ is odd, then we need to find an odd index $j$ such that $nums[j]$ is even, and then swap $nums[i]$ and $nums[j]$. Continue to iterate until $i$ points to the end of the array.
66+
When $i$ points to an even index, if $\textit{nums}[i]$ is odd, we need to find an odd index $j$ such that $\textit{nums}[j]$ is even, and then swap $\textit{nums}[i]$ and $\textit{nums}[j]$. Continue traversing until $i$ reaches the end of the array.
6767

68-
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
68+
The time complexity is $O(n),ドル where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
6969

7070
<!-- tabs:start -->
7171

@@ -154,6 +154,26 @@ function sortArrayByParityII(nums: number[]): number[] {
154154
}
155155
```
156156

157+
#### Rust
158+
159+
```rust
160+
impl Solution {
161+
pub fn sort_array_by_parity_ii(mut nums: Vec<i32>) -> Vec<i32> {
162+
let n = nums.len();
163+
let mut j = 1;
164+
for i in (0..n).step_by(2) {
165+
if nums[i] % 2 != 0 {
166+
while nums[j] % 2 != 0 {
167+
j += 2;
168+
}
169+
nums.swap(i, j);
170+
}
171+
}
172+
nums
173+
}
174+
}
175+
```
176+
157177
#### JavaScript
158178

159179
```js
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn sort_array_by_parity_ii(mut nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut j = 1;
5+
for i in (0..n).step_by(2) {
6+
if nums[i] % 2 != 0 {
7+
while nums[j] % 2 != 0 {
8+
j += 2;
9+
}
10+
nums.swap(i, j);
11+
}
12+
}
13+
nums
14+
}
15+
}

0 commit comments

Comments
(0)

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