MediumNo. 1291
Sequential Digits — LeetCode 1291 Python Solution
- Topics
- Enumeration
- 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
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
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1534 | Count Good Triplets | Easy |
| 1566 | Detect Pattern of Length M Repeated K or More Times | Easy |
| 1620 | Coordinate With Maximum Network Quality | Medium |
| 2735 | Collecting Chocolates | Medium |
| 2765 | Longest Alternating Subarray | Easy |
| 2778 | Sum of Squares of Special Elements | Easy |
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.