2023-04-21 자연수 뒤집어 배열로 만들기
풀이 1
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int[] solution(long n) {
String str = String.valueOf(n);
System.out.println(str);
int[] ans = new int[str.length()];
for (int i=0; i < str.length(); i++) {
ans[str.length()-1-i] = Character.getNumericValue(str.charAt(i));
}
return ans;
}
}
풀이2
1
2
3
4
5
6
7
8
9
10
11
import java.util.*;
class Solution {
public int[] solution(long n) {
String str = Long.toString(n);
String reversed = new StringBuilder(str).reverse().toString();
int[] arr = reversed.chars().map(Character::getNumericValue).toArray();
return arr;
}
}
풀이3
1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
public int[] solution(long n) {
String str = Long.toString(n);
String reversed = new StringBuilder(str).reverse().toString();
char[] arr = reversed.toCharArray();
int[] result = new int[arr.length];
for (int i = 0; i < result.length; i++) {
result[i] = arr[i] - '0';
}
return result;
}
}