Separate the Digits in an Array — LeetCode 2553 Python Solution

EasyArraySimulation
Problem
#2553
Reading time
2 min

The problem

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. To separate the digits of an integer is to get all the digits it has in the same order.

Example

Input
nums = [13,25,83,77]
Output
[1,3,2,5,8,3,7,7]
Explanation
- The separation of 13 is [1,3].

Python solution

Python
class Solution:
    def separateDigits(self, nums: List[int]) -> List[int]:
        ans = []
        for x in nums:
            t = []
            while x:
                t.append(x % 10)
                x //= 10
            ans.extend(t[::-1])
        return ans

Complexity

MeasureComplexity
TimeO(n \times \log_{10} M)
SpaceO(n \times \log_{10} M) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2553. Separate the Digits in an Array?
LeetCode 2553. Separate the Digits in an Array is rated Easy on LeetCode.
What is the time complexity of LeetCode 2553. Separate the Digits in an Array?
The Python solution on this page runs in O(n \times \log_{10} M).
What is the space complexity of LeetCode 2553. Separate the Digits in an Array?
The Python solution on this page uses O(n \times \log_{10} M) auxiliary space.
What topics does LeetCode 2553. Separate the Digits in an Array cover?
LeetCode 2553. Separate the Digits in an Array is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview