Skip to content

Commit

Permalink
922_Sort_Array_By_Parity_II
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyuangong committed Nov 30, 2018
1 parent d8eed51 commit cd1cb10
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Also, there are open source implementations for basic data structs and algorithm
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
| 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) |
| 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). |
| 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). |
| 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) |
| 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) |
| 946 | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/946_Validate_Stack_Sequences.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/946_Validate_Stack_Sequences.java) | Add a stack named inStack to help going through pushed and popped. O(n) and O(n) |
Expand Down
32 changes: 32 additions & 0 deletions java/922_Sort_Array_By_Parity_II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
/*public int[] sortArrayByParityII(int[] A) {
int N = A.length;
int[] ans = new int[N];
int t = 0;
for (int x: A) if (x % 2 == 0) {
ans[t] = x;
t += 2;
}
t = 1;
for (int x: A) if (x % 2 == 1) {
ans[t] = x;
t += 2;
}
return ans;
}*/
public int[] sortArrayByParityII(int[] A) {
int j = 1;
for (int i = 0; i < A.length; i += 2)
if (A[i] % 2 == 1) {
while (A[j] % 2 == 1)
j += 2;

// Swap A[i] and A[j]
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}

return A;
}
}
27 changes: 27 additions & 0 deletions python/922_Sort_Array_By_Parity_II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution(object):
# def sortArrayByParityII(self, A):
# N = len(A)
# ans = [None] * N
# t = 0
# for i, x in enumerate(A):
# if x % 2 == 0:
# ans[t] = x
# t += 2
# t = 1
# for i, x in enumerate(A):
# if x % 2 == 1:
# ans[t] = x
# t += 2
# # We could have also used slice assignment:
# # ans[::2] = (x for x in A if x % 2 == 0)
# # ans[1::2] = (x for x in A if x % 2 == 1)
# return ans

def sortArrayByParityII(self, A):
odd = 1
for i in xrange(0, len(A), 2):
if A[i] % 2:
while A[odd] % 2:
odd += 2
A[i], A[odd] = A[odd], A[i]
return A

0 comments on commit cd1cb10

Please sign in to comment.