Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1. Brute Force
- class Solution:
- def sequentialDigits(self, low: int, high: int) -> List[int]:
- res = []
- #s = '0123456789'
- n = len(str(low))
- #fix the size (2 to 9)
- for i in range(n,10):
- #fix the starting digit
- for j in range(1, 10-i+1):
- #build the number with sequential digits
- #num = int(s[j:i+j])
- num = 0
- for k in range(i):
- num = (num<<3)+(num<<1)+j+k
- if num>high:
- return res
- if num>=low:
- res.append(num)
- return res
- #2. BFS
- class Solution:
- def sequentialDigits(self, low: int, high: int) -> List[int]:
- ans = []
- queue = deque(range(1, 9))
- while queue:
- curr = queue.popleft()
- if (rem := curr%10) != 9:
- num = curr*10+rem+1
- if num > high:
- return ans
- queue.append(num)
- if num >= low:
- ans.append(num)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement