[프로그래머스/정렬] Level 2 가장 큰 수 (JAVA)

Jiwoo Kim·2021년 3월 17일
0

알고리즘 정복하기

목록 보기
30/85
post-thumbnail
post-custom-banner

문제


풀이


코드

import java.util.*;

class Solution {
    
    public String solution(int[] numbers) {
        String[] numberStrings = parseIntoString(numbers);
        Arrays.sort(numberStrings, (o1, o2) -> (o2 + o1).compareTo(o1 + o2));
        if (numberStrings[0].equals("0")) return "0";
        return buildAnswer(numberStrings);
    }

    private String[] parseIntoString(int[] numbers) {
        String[] result = new String[numbers.length];
        for (int i = 0; i < numbers.length; i++) result[i] = String.valueOf(numbers[i]);
        return result;
    }

    private String buildAnswer(String[] numberStrings) {
        StringBuilder answer = new StringBuilder();
        for (String number : numberStrings) answer.append(number);
        return answer.toString();
    }
}
post-custom-banner

0개의 댓글