CodeWars 마흔 다섯 번째 문제

Updated:

Sort the odd

public static int[] sortArray(int[] array) {
    List<Integer> oddList = new ArrayList<Integer>();

    for(int number : array) {
        if(number %2 != 0) {
            oddList.add(number);
        }
    }

    //Collections.sort(oddList);
    oddList.sort(Integer::compareTo);

    for(int i = 0, j = 0 ; i < array.length; i++) {
        if(array[i] % 2 != 0) {
            array[i] = oddList.get(j);
            j++;
        }
    }

    return array;
}
  • Best 알고리즘과 같았다.
  • 여기서 나의 경우 주석으로 처리된 Collection을 사용하였다.
  • 그런데 그 바로 아래의 List에 선언된 걸로 하면 Collection 패키지를 추가하지 않고 List에 있는 기능으로 할 수 있으니 좋을 것 같다.