leetcodedaybyday - Beats 100% with C++ and Beats 83.62% with Python3 and Beats 21.76% with Java
IntuitionThe problem requires extracting digits from each number in the input list and returning them as separate elements in order. A straightforward way to ac
# Intuition
The problem requires extracting digits from each number in the input list and returning them as separate elements in order. A straightforward way to achieve this is by converting each number into a string, iterating through its characters, and converting them back into integers.

# Approach
1. **Convert each number to a string**: This allows easy access to individual digits.
2. **Iterate through the characters of each string**: Extract each digit.
3. **Store the digits in the result array**.
4. **Return the list/array of extracted digits**.

This approach is simple and efficient as it avoids unnecessary computations and maintains the relative order of the digits.

# Complexity
- **Time complexity**:  
  - `O(m)`, where `m` is the total number of digits across all numbers in `nums`.
  - We iterate over each number and process its digits individually.
- **Space complexity**:  
  - `O(m)`, as we store each digit separately in the result array.
---
# Code
```cpp []
class Solution {
public:
    vector<int> separateDigits(vector<int>& nums) {
        vector<int> res;
        for (int i = 0; i < nums.size(); i++){
            string num = to_string(nums[i]);
            for (int j = 0; j < num.size(); j++){
                res.push_back(num[j] - '0');
            }
        }
        return res;
    }
};


```
```java []
class Solution {
    public int[] separateDigits(int[] nums) {
        List<Integer> res = new ArrayList<>();
        for (int num : nums){
            for (char c : String.valueOf(num).toCharArray()){
                res.add(c - '0');
            }
        }
        return res.stream().mapToInt(i -> i).toArray();
    }
}
```
```python3 []
class Solution:
    def separateDigits(self, nums: List[int]) -> List[int]:
        return [int(i) for num in nums for i in str(num)]


```


['Array', 'Simulation', 'C++', 'Java', 'Python3']