1. 풀이
import java.util.*;
class Solution {
public String solution(int[] numbers) {
String answer = "";
String[] arr = new String[numbers.length];
for(int i=0; i < numbers.length; i++){
arr[i] = String.valueOf(numbers[i]);
}
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return (s2+s1).compareTo(s1+s2);
}
});
if (arr[0].equals("0")) return "0";
for (int i = 0; i < arr.length; i++) {
answer+=arr[i];
}
return answer;
}
}
2. 공부해볼것
- Arrats.sort()
정렬하는 함수인데 배열이 들어감
하지만 Collection.reverseOrder() 사용은 Interger 값만 가능함
- comporable compotor 비교
https://makemethink.tistory.com/179
3. 다른사람 풀이
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public String solution(int[] numbers) {
String answer = "";
List<Integer> list = new ArrayList<>();
for(int i = 0; i < numbers.length; i++) {
list.add(numbers[i]);
}
Collections.sort(list, (a, b) -> {
String as = String.valueOf(a), bs = String.valueOf(b);
return -Integer.compare(Integer.parseInt(as + bs), Integer.parseInt(bs + as));
});
StringBuilder sb = _텍스트_new StringBuilder();
for(Integer i : list) {
sb.append(i);
}
answer = sb.toString();
if(answer.charAt(0) == '0') {
return "0";
}else {
return answer;
}
}
}