247 Strobogrammatic Number II
import queue
import copy
class Solution:
def findStrobogrammatic(self, n):
"""
:type n: int
:rtype: List[str]
"""
if not n:
return []
sym_pair1 = {'8': '8', '6': '9', '9': '6', '1': '1', '0': '0'}
sym_pair2 = {'8': '8', '6': '9', '9': '6', '1': '1'}
sym_pair3 = {'8': '8', '1': '1', '0': '0'}
root = [''] * n
q = queue.Queue()
q.put((root, 0, n - 1))
final_result = []
while not q.empty():
parent = q.get()
num = parent[0]
left = parent[1]
right = parent[2]
# 6 or 9 cannot be in the center for odd case
if left == right:
pointer_iter = sym_pair3
# all available options
elif left <= right and left != 0 and right != n - 1:
pointer_iter = sym_pair1
# cannot contain 0s on the edges (doesn't produce unique number)
elif left <= right and left == 0 and right == n - 1:
pointer_iter = sym_pair2
# otherwise the array is full
else:
final_result.append(''.join(num))
continue
# append each option
for key, val in pointer_iter.items():
num[left] = key
num[right] = val
q.put((copy.deepcopy(num), left + 1, right - 1))
return final_result
obj = Solution()
print(obj.findStrobogrammatic(13))Last updated
