
내가 작성한 코드
class Solution {
public int[] solution(long n) {
List<Integer> answer = new ArrayList<>();
while(n > 0) {
answer.add((int) (n % 10));
n /= 10;
}
return answer.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}
다른 사람의 코드
import java.util.stream.IntStream;
class Solution {
public int[] solution(long n) {
return new StringBuilder()
.append(n) // 숫자 n을 문자열로 변환하여 추가
.reverse() // 문자열을 뒤집음 ("54321")
.chars() // 각 문자를 IntStream으로 변환 ('5', '4', ...)
.map(Character::getNumericValue) // 문자형 숫자를 실제 정수값으로 변환 ('5' → 5)
.toArray(); // 스트림을 배열로 변환하여 반환 [5, 4, 3, 2, 1]
}
}
Share article