자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.
n은 10,000,000,000이하인 자연수입니다.
n | result |
---|---|
12345 | [5,4,3,2,1] |
import java.util.*;
class Solution {
public int[] solution(long n) {
int[] answer = {};
ArrayList<Long> temp = new ArrayList<Long>();
while(n > 10){
temp.add(n%10);
n = (n - (n%10))/10;
}
temp.add(n);
answer = new int[temp.size()];
for(int i=0; i<temp.size(); i++){
answer[i] = temp.get(i).intValue();
}
return answer;
}
}