Skip to content

Commit c0e8a15

Browse files
add 905
1 parent 8ea2754 commit c0e8a15

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
3233
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
3334
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
3435
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 905. Sort Array By Parity
5+
*
6+
* Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
7+
*
8+
* You may return any answer array that satisfies this condition.
9+
*
10+
* Example 1:
11+
*
12+
* Input: [3,1,2,4]
13+
* Output: [2,4,3,1]
14+
* The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
15+
*
16+
* Note:
17+
*
18+
* 1 <= A.length <= 5000
19+
* 0 <= A[i] <= 5000
20+
* */
21+
public class _905 {
22+
public static class Solution1 {
23+
public int[] sortArrayByParity(int[] A) {
24+
for (int i = 0, j = A.length - 1; i < j; i++) {
25+
if (A[i] % 2 == 0) {
26+
continue;
27+
} else {
28+
while (j > i && A[j] % 2 != 0) {
29+
j--;
30+
}
31+
swap(A, i, j);
32+
}
33+
}
34+
return A;
35+
}
36+
37+
private void swap(int[] A, int i, int j) {
38+
int tmp = A[i];
39+
A[i] = A[j];
40+
A[j] = tmp;
41+
}
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._905;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _905Test {
9+
private static _905.Solution1 solution1;
10+
private static int[] A;
11+
private static int[] actual;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _905.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{3, 1, 2, 4};
21+
actual = solution1.sortArrayByParity(A);
22+
CommonUtils.printArray(actual);
23+
}
24+
25+
@Test
26+
public void test2() {
27+
A = new int[]{1, 3};
28+
actual = solution1.sortArrayByParity(A);
29+
CommonUtils.printArray(actual);
30+
}
31+
}

0 commit comments

Comments
 (0)