관리자 글쓰기

언어: Java

문제 : 자연수 뒤집어 배열로 만들기

문제 번호 23번 

문제 설명

자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.

제한 조건
  • n은 10,000,000,000이하인 자연수입니다.

 

입출력 예시
n return
12345 [5,4,3,2,1]

Solution.Java

class Solution {
    public int[] solution(long n) {
    int len = (""+n).length();
    int[] arr = new int[len];

    for(int i=0; i<len; i++) {
        arr[i] = (int)(n%10);
        n/=10;
    }
    return arr;
}

}

 

실행 메소드 추가

import java.util.Arrays;
class Solution23 {
    public int[] solution(long n) {
        int len = (""+n).length();
        int[] arr = new int[len];

        for(int i=0; i<len; i++) {
            arr[i] = (int)(n%10);
            n/=10;
        }
        return arr;
    }

    public static void main(String[] args) {
        Solution23 solution23 = new Solution23();
        int n = 12345;
        System.out.println(Arrays.toString(solution23.solution(n)));
    }
}