We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c0bc4ab commit 866f095Copy full SHA for 866f095
Easy/Sort Array By Parity.java
@@ -1,17 +1,16 @@
1
class Solution {
2
- public int[] sortArrayByParity(int[] A) {
3
- int evenIdx = 0;
4
- int oddIdx = A.length - 1;
5
- while (evenIdx < oddIdx) {
6
- if (A[evenIdx] % 2 == 0) {
7
- evenIdx++;
8
- }
9
- else {
10
- int temp = A[evenIdx];
11
- A[evenIdx] = A[oddIdx];
12
- A[oddIdx--] = temp;
13
+ public int[] sortArrayByParity(int[] nums) {
+ int start = 0;
+ int end = 0;
+ int n = nums.length;
+ while (end < n) {
+ if (nums[end] % 2 == 0) {
+ int temp = nums[start];
+ nums[start++] = nums[end];
+ nums[end] = temp;
+ }
+ end++;
14
+ return nums;
15
}
- return A;
16
17
0 commit comments