알고리즘/프로그래머스

[Level 2] 정수 내림차순으로 배치하기

Dev_YooJin 2018. 1. 16. 16:05

출저   https://programmers.co.kr/learn/challenge_codes/118


문제

everseInt 메소드는 int형 n을 매개변수로 입력받습니다. n에 나타나는 숫자를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. n은 양의 정수입니다.


풀이


import java.util.Arrays;
import java.util.Collections;

public class ReverseInt {
    public int reverseInt(int n){

    String[] arr = (Integer.toString(n)).split("");

    Arrays.sort(arr, Collections.reverseOrder());

    return Integer.parseInt(String.join("", arr));
    }

    public static void  main(String[] args){
        ReverseInt ri = new ReverseInt();
        System.out.println(ri.reverseInt(118372));
    }
}