package common;
/**
* @author : zhaoliang
* @program :newCoder
* @description : 调整数组顺序使奇数位于偶数前面
* @create : 2020/11/21 20:08
*/
public class reOrderArray {
//调整整数数组奇数在前偶数在后,稳定(相对位置不变)
public static int[] reOrderArray(int[] array){
if (array.length==0 || array.length==1)return array;
int oddBegin =0;
int oddCount =0;
for (int i = 0; i <array.length ; i++) {
if ((array[i]&1)==1){
oddCount++;
}
}
int[] res = new int[array.length];
for (int i = 0; i <array.length ; i++) {
if ((array[i]&1)==1){
res[oddBegin++] = array[i];
}else {
res[oddCount++] = array[i];
}
}
return res;
}
}
常见算法-调整整数数组奇数在前
最新推荐文章于 2022-01-10 16:01:54 发布