diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /sequential_digits.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'sequential_digits.py')
-rw-r--r-- | sequential_digits.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/sequential_digits.py b/sequential_digits.py new file mode 100644 index 0000000..f84dfcc --- /dev/null +++ b/sequential_digits.py @@ -0,0 +1,32 @@ +# 1291. Sequential Digits + +""" +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. +""" + + +class Solution(object): + def sequentialDigits(self, low, high): + """ + :type low: int + :type high: int + :rtype: List[int] + """ + ans = [] + queue = deque(range(1, 10)) + while queue: + idx = queue.popleft() + if low <= idx <= high: + ans.append(idx) + last = idx % 10 + if last < 9: + queue.append(idx * 10 + last + 1) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.sequentialDigits(low=100, high=300)) + print(obj.sequentialDigits(low=1000, high=13000)) |