[프로그래머스] 정수 내림차순으로 배치하기 (JAVA)

유존돌돌이·2021년 10월 12일
0

Programmers

목록 보기
63/167
post-thumbnail

문제 설명

함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.

제한 조건

n은 1이상 8000000000 이하인 자연수입니다.

Code

import java.util.*;
class Solution {
    public long solution(long n) {
        String s = String.valueOf(n);
        char[] c = s.toCharArray();
        Arrays.sort(c);
        long ret = 0;
        for(int i=c.length-1 ; i>=0 ; i--) {
            ret *= 10;
            ret += (c[i]-'0');
        }
        return ret;
    }
}

0개의 댓글