Separate the Digits in an Array

easy array simulation

Problem

Given an array of positive integers nums, return an array answer that contains the digits of each integer in nums after separating them in the same order they appear. To separate the digits of an integer is to break it into its individual digits, keeping their original ordering.

Inputnums = [13,25,83,77]
Output[1,3,2,5,8,3,7,7]
13 → 1,3; 25 → 2,5; 83 → 8,3; 77 → 7,7, concatenated in order.

def separate_digits(nums):
    answer = []
    for num in nums:
        for ch in str(num):
            answer.append(int(ch))
    return answer
function separateDigits(nums) {
  const answer = [];
  for (const num of nums) {
    for (const ch of String(num)) {
      answer.push(Number(ch));
    }
  }
  return answer;
}
class Solution {
    public int[] separateDigits(int[] nums) {
        List<Integer> answer = new ArrayList<>();
        for (int num : nums) {
            for (char ch : Integer.toString(num).toCharArray()) {
                answer.add(ch - '0');
            }
        }
        int[] res = new int[answer.size()];
        for (int i = 0; i < res.length; i++) res[i] = answer.get(i);
        return res;
    }
}
vector<int> separateDigits(vector<int>& nums) {
    vector<int> answer;
    for (int num : nums) {
        string s = to_string(num);
        for (char ch : s) {
            answer.push_back(ch - '0');
        }
    }
    return answer;
}
Time: O(total digits) Space: O(total digits)