[Programmers] 정수 내림차순으로 배치하기 (Java)
https://programmers.co.kr/learn/courses/30/lessons/12933
Solution
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;
class Solution {
public static long solution(long n) {
long answer = 0;
// long -> long array
long[] list = Stream.of(String.valueOf(n).split("")).mapToLong(Integer::parseInt).toArray();
Long[] listLong = Arrays.stream(list).boxed().toArray(Long[]::new); // long[] -> Long list
Arrays.sort(listLong, Collections.reverseOrder()); // descending
// arr -> string -> delete [ , ]
String strLong = Arrays.toString(listLong).replace(", ", "").replace("[", "").replace("]", "");
answer = Long.parseLong(strLong);
return answer;
}
public static void main(String[] args) {
System.out.println(solution(118372));
}
}
Another Solution
import java.util.*;
class Solution {
public long solution(long n) {
return Long.parseLong(String.valueOf(n).chars().mapToObj(ch -> (char) ch)
.sorted(Comparator.reverseOrder())
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString());
}
}
Leave a comment