Sequential Digits

Problem Id: 1291 Difficulty: Medium Tag: Backtracking


Intuition

There are totally (9 + 8 + ... + 1) = (10) * 9 / 2 = 45 solutions. So we could list all possible solutions and count the result.

Solution


class Solution:
    def sequentialDigits(self, low, high):
        base = '123456789'
        ans = []
        for length in range(2, 10):
            for start in range(len(base) - length + 1):
                tmp = int(base[start: start + length])
                if low <= tmp <= high:
                    ans.append(tmp)
        return ans