File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
LeetCod刷题之旅及题目解析/905.按奇偶排序数组 Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change 77
77
* [ 700.二叉搜索树中的搜索] ( LeetCod刷题之旅及题目解析/700.二叉搜索树中的搜索/700.二叉搜索树中的搜索.md )
78
78
* [ 709.转换成小写字母] ( LeetCod刷题之旅及题目解析/709.转换成小写字母/709.转换成小写字母.md )
79
79
* [ 876.链表的中间结点] ( LeetCod刷题之旅及题目解析/876.链表的中间结点/876.链表的中间结点.md )
80
+ * [ 905.按奇偶排序数组] ( LeetCod刷题之旅及题目解析/905.按奇偶排序数组/905.按奇偶排序数组.md )
80
81
* [ 938.二叉搜索树的范围和] ( LeetCod刷题之旅及题目解析/938.二叉搜索树的范围和/938.二叉搜索树的范围和.md )
81
82
* [ 977.有序数组的平方] ( LeetCod刷题之旅及题目解析/977.有序数组的平方/977.有序数组的平方.md )
82
83
* [ 1021.删除最外层的括号] ( LeetCod刷题之旅及题目解析/1021.删除最外层的括号/1021.删除最外层的括号.md )
You can’t perform that action at this time.
0 commit comments