Skip to content
LC-1291 Medium LeetCode

1291. Sequential Digits

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 65% Topics: Enumeration
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O((8 + 1) * 8 / 2) = O(1)
# Space: O(8) = O(1)

import collections


class Solution(object):
    def sequentialDigits(self, low, high):
        """
        :type low: int
        :type high: int
        :rtype: List[int]
        """
        result = []
        q = collections.deque(range(1, 9))
        while q:
            num = q.popleft()
            if num > high:
                continue
            if low <= num:
                result.append(num)
            if num%10+1 < 10:
                q.append(num*10+num%10+1)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT