1291-sequential-digits¶
Try it on leetcode
Description¶
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 1:
Input: low = 100, high = 300 Output: [123,234]
Example 2:
Input: low = 1000, high = 13000 Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9
Solution(Python)¶
class Solution:
# def isSequential(self,num):
# candidate = num
# candidate,after = divmod(candidate,10)
# while candidate > 0:
# candidate,before = divmod(candidate,10)
# if after != before+1:
# return False
# after = before
# return True
# def sequentialDigits(self, low: int, high: int) -> List[int]:
# res = []
# for num in range(low,high):
# if self.isSequential(num):
# res.append(num)
# return res
# Time Complexity: O(M-N)
def sequentialDigits(self, low, high):
out = []
string = "123456789"
for i in range(len(str(low)), len(str(high)) + 1):
for j in range(10 - i):
num = int(string[j: j + i])
if low <= num and num <= high:
out.append(num)
return out
# Time Complexity: O(1)
# Space Complexity: O(1)