06
28
728x90

문제 설명

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

제한 조건
  • n은 10,000,000,000이하인 자연수입니다.
입출력 예
n return
12345 [5,4,3,2,1]
class Solution {
    public int[] solution(long n) {
        String num = String.valueOf(n); //long변수에 저장된 숫자를 string으로 변환
        int lenght = num.length(); //숫자의 길이 저장
        int[] answer = new int[lenght]; //답을 저장할 배열(int)
        String[] string_array = new String[lenght]; //string배열
        for(int i = 0; i<lenght; i++) //숫자의 길이만큼 반복
        {
            string_array[i] = num.substring(lenght-i-1,lenght-i); //가장 끝자리의 숫자를 하나씩 저장
            answer[i] = Integer.parseInt(string_array[i]);//string->int 형변환
        }
        return answer;
    }
}

 

728x90
COMMENT