Skip to content

Commit e41ebf5

Browse files
committed
feat: update solutions to leetcode problem: No.0905. Sort Array By Parity
1 parent 98d5bd8 commit e41ebf5

File tree

5 files changed

+154
-69
lines changed

5 files changed

+154
-69
lines changed

solution/0900-0999/0905.Sort Array By Parity/README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,83 @@
2828
<li><code>0 &lt;= A[i] &lt;= 5000</code></li>
2929
</ol>
3030

31-
3231
## 解法
3332

3433
<!-- 这里可写通用的实现逻辑 -->
3534

35+
双指针原地交换数组元素。
36+
3637
<!-- tabs:start -->
3738

3839
### **Python3**
3940

4041
<!-- 这里可写当前语言的特殊实现逻辑 -->
4142

4243
```python
43-
44+
class Solution:
45+
def sortArrayByParity(self, A: List[int]) -> List[int]:
46+
i, j = 0, len(A) - 1
47+
while i < j:
48+
if (A[i] & 1) > (A[j] & 1):
49+
A[i], A[j] = A[j], A[i]
50+
if A[i] & 1 == 0:
51+
i += 1
52+
if A[j] & 1 == 1:
53+
j -= 1
54+
return A
4455
```
4556

4657
### **Java**
4758

4859
<!-- 这里可写当前语言的特殊实现逻辑 -->
4960

5061
```java
62+
class Solution {
63+
public int[] sortArrayByParity(int[] A) {
64+
int i = 0, j = A.length - 1;
65+
while (i < j) {
66+
if ((A[i] & 1) > (A[j] & 1)) {
67+
int t = A[i];
68+
A[i] = A[j];
69+
A[j] = t;
70+
}
71+
if ((A[i] & 1) == 0) {
72+
++i;
73+
}
74+
if ((A[j] & 1) == 1) {
75+
--j;
76+
}
77+
}
78+
return A;
79+
}
80+
}
81+
```
5182

83+
### **JavaScript**
84+
85+
```js
86+
/**
87+
* @param {number[]} A
88+
* @return {number[]}
89+
*/
90+
var sortArrayByParity = function (A) {
91+
let i = 0;
92+
let j = A.length - 1;
93+
while (i < j) {
94+
if ((A[i] & 1) > (A[j] & 1)) {
95+
const t = A[i];
96+
A[i] = A[j];
97+
A[j] = t;
98+
}
99+
if ((A[i] & 1) == 0) {
100+
++i;
101+
}
102+
if ((A[j] & 1) == 1) {
103+
--j;
104+
}
105+
}
106+
return A;
107+
};
52108
```
53109

54110
### **...**

solution/0900-0999/0905.Sort Array By Parity/README_EN.md

+56-17
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
<p>Given an array <code>A</code> of non-negative integers, return an array consisting of all the even elements of <code>A</code>, followed by all the odd elements of <code>A</code>.</p>
88

9-
10-
119
<p>You may return any answer array that satisfies this condition.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

17-
18-
1913
<div>
2014

2115
<p><strong>Example 1:</strong></p>
2216

23-
24-
2517
<pre>
2618

2719
<strong>Input: </strong><span id="example-input-1-1">[3,1,2,4]</span>
@@ -32,39 +24,86 @@ The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
3224

3325
</pre>
3426

35-
36-
3727
<p>&nbsp;</p>
3828

39-
40-
4129
<p><strong>Note:</strong></p>
4230

43-
44-
4531
<ol>
4632
<li><code>1 &lt;= A.length &lt;= 5000</code></li>
4733
<li><code>0 &lt;= A[i] &lt;= 5000</code></li>
4834
</ol>
4935

5036
</div>
5137

52-
53-
5438
## Solutions
5539

5640
<!-- tabs:start -->
5741

5842
### **Python3**
5943

6044
```python
61-
45+
class Solution:
46+
def sortArrayByParity(self, A: List[int]) -> List[int]:
47+
i, j = 0, len(A) - 1
48+
while i < j:
49+
if (A[i] & 1) > (A[j] & 1):
50+
A[i], A[j] = A[j], A[i]
51+
if A[i] & 1 == 0:
52+
i += 1
53+
if A[j] & 1 == 1:
54+
j -= 1
55+
return A
6256
```
6357

6458
### **Java**
6559

6660
```java
61+
class Solution {
62+
public int[] sortArrayByParity(int[] A) {
63+
int i = 0, j = A.length - 1;
64+
while (i < j) {
65+
if ((A[i] & 1) > (A[j] & 1)) {
66+
int t = A[i];
67+
A[i] = A[j];
68+
A[j] = t;
69+
}
70+
if ((A[i] & 1) == 0) {
71+
++i;
72+
}
73+
if ((A[j] & 1) == 1) {
74+
--j;
75+
}
76+
}
77+
return A;
78+
}
79+
}
80+
```
6781

82+
### **JavaScript**
83+
84+
```js
85+
/**
86+
* @param {number[]} A
87+
* @return {number[]}
88+
*/
89+
var sortArrayByParity = function (A) {
90+
let i = 0;
91+
let j = A.length - 1;
92+
while (i < j) {
93+
if ((A[i] & 1) > (A[j] & 1)) {
94+
const t = A[i];
95+
A[i] = A[j];
96+
A[j] = t;
97+
}
98+
if ((A[i] & 1) == 0) {
99+
++i;
100+
}
101+
if ((A[j] & 1) == 1) {
102+
--j;
103+
}
104+
}
105+
return A;
106+
};
68107
```
69108

70109
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int[] sortArrayByParity(int[] A) {
3+
int i = 0, j = A.length - 1;
4+
while (i < j) {
5+
if ((A[i] & 1) > (A[j] & 1)) {
6+
int t = A[i];
7+
A[i] = A[j];
8+
A[j] = t;
9+
}
10+
if ((A[i] & 1) == 0) {
11+
++i;
12+
}
13+
if ((A[j] & 1) == 1) {
14+
--j;
15+
}
16+
}
17+
return A;
18+
}
19+
}

solution/0900-0999/0905.Sort Array By Parity/Solution.js

+11-43
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,21 @@
22
* @param {number[]} A
33
* @return {number[]}
44
*/
5-
// 第一次的做法
65
var sortArrayByParity = function (A) {
7-
const len = A.length;
8-
if (len == 1) return A;
9-
let evenNumber = [];
10-
let oddNumber = [];
11-
for (let i = 0; i < len; i++) {
12-
if (A[i] % 2 == 0) {
13-
evenNumber.push(A[i]);
14-
}
15-
if (A[i] % 2 != 0) {
16-
oddNumber.push(A[i]);
17-
}
18-
}
19-
return evenNumber.concat(oddNumber);
20-
};
21-
// 修改第一次的代码,只使用一个数组,减少一次合并数组操作
22-
var sortArrayByParity = function (A) {
23-
const len = A.length;
24-
if (len == 1) return A;
25-
let eoNum = [];
26-
for (let i = 0; i < len; i++) {
27-
if (A[i] % 2 == 1) {
28-
eoNum.push(A[i]);
6+
let i = 0;
7+
let j = A.length - 1;
8+
while (i < j) {
9+
if ((A[i] & 1) > (A[j] & 1)) {
10+
const t = A[i];
11+
A[i] = A[j];
12+
A[j] = t;
2913
}
30-
if (A[i] % 2 == 0) {
31-
eoNum.unshift(A[i]);
14+
if ((A[i] & 1) == 0) {
15+
++i;
3216
}
33-
}
34-
return eoNum;
35-
};
36-
// 双指针做法,无需新数组,push和unshift操作,利用第三个变量进行交换
37-
var sortArrayByParity = function (A) {
38-
const len = A.length;
39-
if (len == 1) return A;
40-
let i = 0,
41-
j = len - 1;
42-
while (i < j) {
43-
if (A[i] % 2 == 1 && A[j] % 2 == 0) {
44-
let temp = A[j];
45-
A[j] = A[i];
46-
A[i] = temp;
47-
i++;
48-
j--;
17+
if ((A[j] & 1) == 1) {
18+
--j;
4919
}
50-
if (A[i] % 2 == 0) i++;
51-
if (A[j] % 2 == 1) j--;
5220
}
5321
return A;
5422
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
class Solution:
2-
def sortArrayByParity(self, A):
3-
"""
4-
:type A: List[int]
5-
:rtype: List[int]
6-
"""
7-
8-
return sorted(A, key = lambda i: i % 2)
2+
def sortArrayByParity(self, A: List[int]) -> List[int]:
3+
i, j = 0, len(A) - 1
4+
while i < j:
5+
if (A[i] & 1) > (A[j] & 1):
6+
A[i], A[j] = A[j], A[i]
7+
if A[i] & 1 == 0:
8+
i += 1
9+
if A[j] & 1 == 1:
10+
j -= 1
11+
return A

0 commit comments

Comments
 (0)