922. 按奇偶排序数组 II
Problem:
思路
遍历一遍,分别给两个指针,分别赋值奇数和偶数,步长为二。
Code
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int len = nums.length;
//创建一个与nums长度相同的数组
int[] res = new int[len];
int o = 0;
int j = 1;
for (int i = 0; i < len; i++) {
//非奇即偶,步长为2
if (nums[i] % 2 == 0) {
res[o] = nums[i];
o += 2;
} else {
res[j] = nums[i];
j += 2;
}
}
return res;
}
}