A Python Solution


  • 0
    S

    Please feel free to point out the code optimization. Thanks.

    class Solution(object):
        def findStrobogrammatic(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
    
            def build(n, acc, result):
                l = len(acc)
                if n == 0:
                    if l > 0 and acc[0] != '0':
                        result.append(acc)
                    return
    
                if n == 1:
                    if l > 0 and acc[0] == '0': return
                    for c in ['0', '1', '8']:
                        result.append(acc[:int(l / 2)] + c + acc[int(l / 2):])
                    return
    
                for (x, y) in [('0', '0'), ('1', '1'), ('6', '9'), ('9', '6'), ('8', '8')]:
                    build(n - 2, x + acc + y, result)
    
            if n == 0: return [""]
    
            acc, result = "", []
            build(n, acc, result)
    
            return result

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.