Python using iterative method


  • 0
    D

    class Solution(object):
    def findStrobogrammatic(self, n):
    """
    :type n: int
    :rtype: List[str]
    """

        def helper(total, depth, number, res):
            if depth == total / 2 :
                if total%2 == 1:
                    number[depth] = 8
                    res.append("".join(map(str, number)))
                    number[depth] = 0
                    res.append("".join(map(str, number)))
                    number[depth] = 1
                    res.append("".join(map(str, number)))
                    return
                else:
                    res.append("".join(map(str, number)))
                    return
    
            # 0, 0
            # 6, 9
            # 8, 8
            # 9, 6
            for i, l in enumerate(loop):
                if depth == 0 and i == 0:
                    continue
                number[depth] = l[0]
                number[total - depth - 1] = l[1]
                helper(total, depth+1, number, res)
    
        loop = [[0, 0], [6, 9], [8, 8], [9, 6], [1, 1]]
    
        res = []
        number = [-1] * n
        helper(n, 0, number, res)
        return res

Log in to reply
 

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