[프로그래머스/Java] Lv.1 자연수 뒤집어 배열로 만들기

febCho·2024년 6월 24일
0

코딩테스트

목록 보기
229/253
post-thumbnail

문제

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

- 제한사항

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

풀이

class Solution {
    public int[] solution(long n) {
        String nStr = String.valueOf(n);
        int[] answer = new int[nStr.length()];
        int idx = 0;
        
        for(int i=nStr.length() - 1; i>=0; i--){
            answer[idx++] = Integer.parseInt(String.valueOf(nStr.charAt(i)));
        }
        
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글