Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd1cb10

Browse files
committed
922_Sort_Array_By_Parity_II
1 parent d8eed51 commit cd1cb10

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Also, there are open source implementations for basic data structs and algorithm
160160
| 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. |
161161
| 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) |
162162
| 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). |
163+
| 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). |
163164
| 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) |
164165
| 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) |
165166
| 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) |

java/922_Sort_Array_By_Parity_II.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[] sortArrayByParityII(int[] A) {
3+
int N = A.length;
4+
int[] ans = new int[N];
5+
int t = 0;
6+
for (int x: A) if (x % 2 == 0) {
7+
ans[t] = x;
8+
t += 2;
9+
}
10+
t = 1;
11+
for (int x: A) if (x % 2 == 1) {
12+
ans[t] = x;
13+
t += 2;
14+
}
15+
return ans;
16+
}*/
17+
public int[] sortArrayByParityII(int[] A) {
18+
int j = 1;
19+
for (int i = 0; i < A.length; i += 2)
20+
if (A[i] % 2 == 1) {
21+
while (A[j] % 2 == 1)
22+
j += 2;
23+
24+
// Swap A[i] and A[j]
25+
int tmp = A[i];
26+
A[i] = A[j];
27+
A[j] = tmp;
28+
}
29+
30+
return A;
31+
}
32+
}

python/922_Sort_Array_By_Parity_II.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
# def sortArrayByParityII(self, A):
3+
# N = len(A)
4+
# ans = [None] * N
5+
# t = 0
6+
# for i, x in enumerate(A):
7+
# if x % 2 == 0:
8+
# ans[t] = x
9+
# t += 2
10+
# t = 1
11+
# for i, x in enumerate(A):
12+
# if x % 2 == 1:
13+
# ans[t] = x
14+
# t += 2
15+
# # We could have also used slice assignment:
16+
# # ans[::2] = (x for x in A if x % 2 == 0)
17+
# # ans[1::2] = (x for x in A if x % 2 == 1)
18+
# return ans
19+
20+
def sortArrayByParityII(self, A):
21+
odd = 1
22+
for i in xrange(0, len(A), 2):
23+
if A[i] % 2:
24+
while A[odd] % 2:
25+
odd += 2
26+
A[i], A[odd] = A[odd], A[i]
27+
return A

0 commit comments

Comments
 (0)