Created
February 25, 2019 07:17
-
-
Save mikeyang01/18216282e5e20a9c1bf34b52b950735e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分, | |
//所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 | |
import java.util.ArrayList; | |
public class Even_Odd_Array { | |
static int[] array = {5, 6, 7, 8}; | |
public static class Solution { | |
public static void reOrderArray(int[] array) { | |
ArrayList<Integer> list1 = new ArrayList(); | |
ArrayList<Integer> list2 = new ArrayList(); | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] % 2 == 1) | |
list1.add(array[i]); | |
else | |
list2.add(array[i]); | |
} | |
for (int i = 0; i < array.length; i++) { | |
if (i < list1.size()) | |
array[i] = list1.get(i); | |
else { | |
array[i] = list2.get(i-list1.size()); | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
Solution.reOrderArray(array); | |
for (int i = 0; i < array.length; i++) { | |
System.out.println(array[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment