-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
40 lines (36 loc) · 1.01 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ds.pointer.leetcode905;
import java.util.Arrays;
/**
* 按奇偶排序数组
* LeetCode 905 https://leetcode-cn.com/problems/sort-array-by-parity/
*
* @author yangyi 2022年04月24日19:17:01
*/
public class Solution {
public int[] sortArrayByParity(int[] nums) {
if (nums == null || nums.length == 0) {
return new int[]{0};
}
int start = 0;
int end = nums.length - 1;
while (start < end) {
if (nums[start] % 2 == 0) {
start++;
continue;
}
if (nums[end] % 2 != 0) {
end--;
continue;
}
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
return nums;
}
public static void main(String[] args) {
int[] nums = {3, 1, 2, 4};
Solution sortArrayByParity = new Solution();
System.out.println(Arrays.toString(sortArrayByParity.sortArrayByParity(nums)));
}
}