Skip to content

Commit 6fe4760

Browse files
committed
905_Sort_Array_By_Parity
1 parent f6325f5 commit 6fe4760

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ Also, there are open source implementations for basic data structs and algorithm
170170
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
171171
| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/852_Peak_Index_in_a_Mountain_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/852_Peak_Index_in_a_Mountain_Array.java) | 1. Scan the array until encountering decline, O(n) and O(1)<br>2. Binary seach with additional check for [i + 1], O(logn) and O(1)|
172172
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/876_Middle_of_the_Linked_List.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/876_Middle_of_the_Linked_List.java) | 1. Copy to array, O(n) and O(n)<br>2. Fast and slow point, where fast point is 2 times faster than slow point, O(n) and O(1) |
173-
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). |
173+
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). |
174+
| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/905_Sort_Array_By_Parity.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/905_Sort_Array_By_Parity.java) | 1. Sort with condition, O(nlogn) and O(1)<br>2. Scan all and split odd and even number into different array, O(n) and O(n)<br>3. In place swap similar to quick sort, O(n) and O(1) |
174175
| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/922_Sort_Array_By_Parity_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/922_Sort_Array_By_Parity_II.java) | 1. Place odd and even number in odd and even place, not sort is needed. O(n) and O(1)<br>2. Two points with quick sort swap idea, O(n) and O(1). |
175176
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
176177
| 945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/945_Minimum_Increment_to_Make_Array_Unique.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/945_Minimum_Increment_to_Make_Array_Unique.java) | Sort, then list duplicate and missing value in sorted list. O(nlgn) and O(n) |

java/905_Sort_Array_By_Parity.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
/*public int[] sortArrayByParity(int[] A) {
3+
Array.sort(A, (a, b)-> Integer.compare(a%2, b%2));
4+
return A;
5+
}*/
6+
7+
/*public int[] sortArrayByParity(int[] A) {
8+
int[] ans = new int[A.length];
9+
int pos = 0;
10+
for (int num: A)
11+
if (num % 2 == 0)
12+
ans[pos++] = num;
13+
for (int num: A)
14+
if (num % 2 == 1)
15+
ans[pos++] = num;
16+
return ans;
17+
}*/
18+
19+
public int[] sortArrayByParity(int[] A) {
20+
int lo = 0, hi = A.length - 1;
21+
while (lo < hi) {
22+
if (A[lo] % 2 > A[hi] % 2) {
23+
int tmp = A[hi];
24+
A[hi] = A[lo];
25+
A[lo] = tmp;
26+
}
27+
if (A[lo] % 2 == 0) lo++;
28+
if (A[hi] % 2 == 1) hi--;
29+
}
30+
return A;
31+
}
32+
}

python/905_Sort_Array_By_Parity.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
# def sortArrayByParity(self, A):
3+
# """
4+
# :type A: List[int]
5+
# :rtype: List[int]
6+
# """
7+
# # Bad idea, O(nlogn)
8+
# A.sort(key=lambda x: x % 2)
9+
# return A
10+
11+
# def sortArrayByParity(self, A):
12+
# return ([x for x in A if x % 2 == 0] +
13+
# [x for x in A if x % 2 == 1])
14+
15+
def sortArrayByParity(self, A):
16+
# Quit like quick sort or quick selection
17+
lo, hi = 0, len(A) - 1
18+
while lo < hi:
19+
if A[lo] % 2 > A[hi] % 2:
20+
A[lo], A[hi] = A[hi], A[lo]
21+
if A[lo] % 2 == 0: lo += 1
22+
if A[hi] % 2 == 1: hi -= 1
23+
return A

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy