Python 2553.py
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
for n in nums:
for c in str(n):
yield int(c)
Difficulty
and has an acceptance rate of about 86% (200k+ submissions).
Typical patterns to solve this problem:
- Array
- Simulation
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a Solution class with a single method, separateDigits. The method receives a collection of integers and produces a flat sequence of the individual decimal digits that compose each integer. By employing a generator (yield), the routine streams digits one by one, allowing the caller to consume the result lazily without allocating a full list up front.
Core Logic
The method consists of two nested loops. The outer loop walks through each integer in the input collection:
for n in nums:
Inside the outer loop, the integer n is transformed into its string representation. This conversion makes each decimal digit accessible as a character:
for c in str(n):
The inner loop iterates over the characters of the string. Each character, which is guaranteed to be a digit character for non-negative inputs, is converted back to an integer and emitted to the caller:
yield int(c)
Collectively, these three snippets form the complete algorithm.
Edge-Case Handling
When the input list is empty, the outer loop never executes, and the generator produces no output, which correctly represents the absence of digits. For numbers that consist of a single digit, the string conversion yields a one-character string, and the inner loop yields that single digit. The routine also processes numbers that contain the digit zero; the character '0' is converted to the integer 0 and yielded without special treatment. Because the conversion relies on the string form of each integer, any integer that can be represented in base-10-including very large values-will be handled correctly as long as the string conversion succeeds.
Performance Characteristics
The use of a generator (yield) is a deliberate optimization. Instead of constructing an intermediate list that holds all digits, the method produces each digit on demand. This approach reduces peak memory consumption, especially when the input contains many large numbers. The conversion str(n) is performed once per integer, and the subsequent iteration over its characters avoids repeated arithmetic operations such as division and modulo that are typical in digit extraction algorithms. By leveraging Python’s built-in string handling, the implementation benefits from highly optimized C-level code for both the conversion and the character iteration.
Overall, the design balances simplicity with efficiency: the algorithm is straightforward to read, and the lazy generation of results ensures that resource usage scales with the actual consumption pattern of the caller.
ELI5 - Simplified Explanation
Imagine having a big collection of colorful blocks where each block has a number painted on it. Some of the blocks have two digits, like a ten, which is a one and a zero stuck together. This code acts like a little helper that looks at every block in the collection. When it finds a block with two digits, it carefully pulls them apart so they become two separate, smaller blocks. It keeps doing this for every single number in the collection until every digit is standing all by itself in a new line. It is like taking a double-scoop ice cream cone and splitting it into two separate scoops so everyone can have their own.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||
| www | AlgoMonster | C++ | Java | Python | TS | ||||
| WalkCCC | C++ | Java | Python | ||||||
| leetcode.ca | C++ | Java | Python | C | Go | Rust | TS | ||
| Progiez | C++ | Java | Python | ||||||
| github | charles-wangkai | Java | |||||||
| fsq | C++ | ||||||||
| JoshCrozier | JS | ||||||||
| lkwq007 | Python | ||||||||
| qiyuangong | C++ | ||||||||
| varunu28 | Java | ||||||||
| withaarzoo | C++ | ||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||
| WalkCCC | C++, Python, Java | ||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | ||||||||
| Progiez | C++, Java, Python | ||||||||
| charles-wangkai | Java | ||||||||
| fsq | C++ | ||||||||
| JoshCrozier | JavaScript | ||||||||
| lkwq007 | Python | ||||||||
| qiyuangong | C++ | ||||||||
| varunu28 | Java | ||||||||
| withaarzoo | C++ |
Videos
Similar Problems
- problem 2180: Count Integers With Even Digit Sum
- problem 2544: Alternating Digit Sum
Solved On The Same Day
- problem 1260: Shift 2D Grid
- problem 1309: Decrypt String from Alphabet to Integer Mapping
- problem 2520: Count the Digits That Divide a Number
- problem 2815: Max Pair Sum in an Array
- problem 2833: Furthest Point From Origin
- problem 921: Minimum Add to Make Parentheses Valid
- problem 986: Interval List Intersections
- problem 1003: Check If Word Is Valid After Substitutions
- problem 564: Find the Closest Palindrome
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality