Difficulty: Easy | Acceptance: 80.90% | Paid: No Topics: Array, Simulation
Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums.
- Examples
- Constraints
- String Conversion
- Mathematical Extraction (Reverse)
- Mathematical Extraction (Pre-calculate)
- Stack Approach
Examples
Example 1
Input: nums = [13,25,83,77] Output: [1,3,2,5,8,3,7,7] Explanation:
- 13 is separated into [1, 3].
- 25 is separated into [2, 5].
- 83 is separated into [8, 3].
- 77 is separated into [7, 7].
- answer is concatenated as [1,3,2,5,8,3,7,7].
Example 2
Input: nums = [7,1,3,9] Output: [7,1,3,9] Explanation: Each number in nums is a single digit, so no separation is needed.
Constraints
1 <= nums.length <= 1000
1 <= nums[i] <= 10⁵
String Conversion
Intuition Convert each number to a string and iterate through each character, converting back to integer.
Steps
- Initialize an empty result array
- For each number in the input array:
- Convert the number to a string
- For each character in the string:
- Convert the character to an integer
- Add to the result array
- Return the result array
from typing import List
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
result = []
for num in nums:
for digit in str(num):
result.append(int(digit))
return resultComplexity
- Time: O(n × d) where n is the number of elements and d is the average number of digits per element
- Space: O(n × d) for the output array, plus O(d) for the string conversion of each element
- Notes: Simple and readable, but involves string conversion overhead
Mathematical Extraction (Reverse)
Intuition Extract digits from right to left using modulo and division, store in a temporary array, then reverse to get the correct order.
Steps
- Initialize an empty result array
- For each number in the input array:
- Initialize an empty temporary array
- While the number is greater than 0:
- Extract the last digit using modulo 10
- Add to the temporary array
- Divide the number by 10
- Reverse the temporary array
- Add all elements to the result array
- Return the result array
from typing import List
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
result = []
for num in nums:
temp = []
while num > 0:
temp.append(num % 10)
num //= 10
result.extend(reversed(temp))
return resultComplexity
- Time: O(n × d) where n is the number of elements and d is the average number of digits per element
- Space: O(n × d) for the output array, plus O(d) for the temporary array used to reverse digits
- Notes: Pure mathematical approach without string conversion, but requires extra space for reversal
Mathematical Extraction (Pre-calculate)
Intuition Count the number of digits first, then extract digits from left to right by dividing by appropriate powers of 10.
Steps
- Initialize an empty result array
- For each number in the input array:
- Count the number of digits
- Calculate the divisor as 10^(digits-1)
- While the divisor is greater than 0:
- Extract the leftmost digit by dividing by the divisor
- Add to the result array
- Update the number by taking modulo divisor
- Divide the divisor by 10
- Return the result array
from typing import List
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
result = []
for num in nums:
temp = num
digits = 0
while temp > 0:
digits += 1
temp //= 10
divisor = 10 ** (digits - 1)
while divisor > 0:
result.append(num // divisor)
num %= divisor
divisor //= 10
return resultComplexity
- Time: O(n × d) where n is the number of elements and d is the average number of digits per element
- Space: O(n × d) for the output array, plus O(1) extra space
- Notes: Extracts digits in correct order without extra reversal space, but requires two passes per number
Stack Approach
Intuition Extract digits from right to left and push to a stack, then pop from the stack to get the left-to-right order.
Steps
- Initialize an empty result array
- For each number in the input array:
- Initialize an empty stack
- While the number is greater than 0:
- Extract the last digit using modulo 10
- Push to the stack
- Divide the number by 10
- While the stack is not empty:
- Pop from the stack
- Add to the result array
- Return the result array
from typing import List
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
result = []
for num in nums:
stack = []
while num > 0:
stack.append(num % 10)
num //= 10
while stack:
result.append(stack.pop())
return resultComplexity
- Time: O(n × d) where n is the number of elements and d is the average number of digits per element
- Space: O(n × d) for the output array, plus O(d) for the stack used to reverse digits
- Notes: Uses stack data structure for natural LIFO reversal, conceptually clear but similar to array reversal approach