Sequential Digits — LeetCode 1291 Python Solution
MediumEnumeration
- Problem
- #1291
- Reading time
- 2 min
- Source
- leetcode.com
The problem
An integer has sequential digits if and only if each digit in the number is one more than the previous digit. Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example
- Input
- low = 100, high = 300
- Output
- [123,234]
Python solution
Python
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
ans = []
for i in range(1, 9):
x = i
for j in range(i + 1, 10):
x = x * 10 + j
if low <= x <= high:
ans.append(x)
return sorted(ans)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1291. Sequential Digits?
- LeetCode 1291. Sequential Digits is rated Medium on LeetCode.
- What topics does LeetCode 1291. Sequential Digits cover?
- LeetCode 1291. Sequential Digits is tagged Enumeration on LeetCode.