Skip to content

Commit 11450b7

Browse files
committed
📝 docs : add 905. Sort Array By Parity.md
1 parent 21c1987 commit 11450b7

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 905. 按奇偶排序数组
2+
> https://leetcode-cn.com/problems/sort-array-by-parity/
3+
4+
5+
### Java
6+
```java
7+
/*
8+
* @Author: Goog Tech
9+
* @Date: 2020-08-16 11:39:16
10+
* @LastEditTime: 2020-08-16 11:39:27
11+
* @Description: https://leetcode-cn.com/problems/sort-array-by-parity/
12+
* @FilePath: \leetcode-googtech\#905. Sort Array By Parity\Solution.java
13+
* @WebSite: https://algorithm.show/
14+
*/
15+
16+
class Solution {
17+
// 双指针法
18+
public int[] sortArrayByParity(int[] A) {
19+
int left = 0, right = A.length - 1;
20+
while(left < right) {
21+
while(left < right && A[left] % 2 == 0) left++;
22+
while(left < right && A[right] % 2 != 0) right--;
23+
int temp = A[left];
24+
A[left] = A[right];
25+
A[right] = temp;
26+
}
27+
return A;
28+
}
29+
}
30+
```
31+
32+
### Python
33+
```python
34+
'''
35+
Author: Goog Tech
36+
Date: 2020-08-16 11:39:20
37+
LastEditTime: 2020-08-16 11:39:46
38+
Description: https://leetcode-cn.com/problems/sort-array-by-parity/
39+
FilePath: \leetcode-googtech\#905. Sort Array By Parity\Solution.py
40+
WebSite: https://algorithm.show/
41+
'''
42+
43+
class Solution(object):
44+
def sortArrayByParity(self, A):
45+
"""
46+
:type A: List[int]
47+
:rtype: List[int]
48+
"""
49+
i = 0
50+
for j in range(len(A)):
51+
if A[j] % 2 == 0:
52+
A[i], A[j] = A[j], A[i]
53+
i += 1
54+
return A
55+
```

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* [700.二叉搜索树中的搜索](LeetCod刷题之旅及题目解析/700.二叉搜索树中的搜索/700.二叉搜索树中的搜索.md)
7878
* [709.转换成小写字母](LeetCod刷题之旅及题目解析/709.转换成小写字母/709.转换成小写字母.md)
7979
* [876.链表的中间结点](LeetCod刷题之旅及题目解析/876.链表的中间结点/876.链表的中间结点.md)
80+
* [905.按奇偶排序数组](LeetCod刷题之旅及题目解析/905.按奇偶排序数组/905.按奇偶排序数组.md)
8081
* [938.二叉搜索树的范围和](LeetCod刷题之旅及题目解析/938.二叉搜索树的范围和/938.二叉搜索树的范围和.md)
8182
* [977.有序数组的平方](LeetCod刷题之旅及题目解析/977.有序数组的平方/977.有序数组的平方.md)
8283
* [1021.删除最外层的括号](LeetCod刷题之旅及题目解析/1021.删除最外层的括号/1021.删除最外层的括号.md)

0 commit comments

Comments
 (0)