Home 프로그래머스 정수 내림차순으로 배치하기
Post
Cancel

프로그래머스 정수 내림차순으로 배치하기

프로그래머스 정수 내림차순으로 배치하기

풀이

  • 소요 시간 약 5분

내가 푼 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;

class Solution {
    public long solution(long n) {
        String str = Long.toString(n);
        PriorityQueue<String> pq = new PriorityQueue<>(Collections.reverseOrder());
        
        for(int i=0; i<str.length(); i++) pq.add(Integer.toString(str.charAt(i)-48));
        
        String ans ="";
        while(!pq.isEmpty()) ans+= pq.poll(); 
        
        return Long.parseLong(ans);
    }
}
This post is licensed under CC BY 4.0 by the author.