LeetCode 2553: Separate the Digits in an Array
Problem Description
Explanation
To solve this problem, we need to iterate through each integer in the input array, separate its digits, and then add those separated digits to the output array. We can achieve this by converting each integer to a string, and then iterating over each character in the string to add it to the output array.
- Time complexity: O(N * M), where N is the number of integers in the input array and M is the maximum number of digits in any integer.
- Space complexity: O(N * M), for the output array where we store separated digits.
Solutions
class Solution {
public int[] separateDigits(int[] nums) {
List<Integer> result = new ArrayList<>();
for (int num : nums) {
String numStr = String.valueOf(num);
for (char c : numStr.toCharArray()) {
result.add(Character.getNumericValue(c));
}
}
int[] output = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
output[i] = result.get(i);
}
return output;
}
}
Loading editor...