Python solution (not that great!) but intuitive


  • 0
    D
    class Solution(object):
        def findStrobogrammatic(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            # This list is for strobogrammatic # which can be used.
            # Except 6 and 9, which we need to use in pair
            self.rev = [0, 1, 8, 6, 9]
            if n == 1:
                return ['0', '1', '8']
            self.res = list()
            self.helper(n)
            return self.res
    
        def helper(self, n, start=0, temp=None):
            if not temp:
                temp = [None]*n
            if not n:
                self.res.append(''.join(str(t) for t in temp))
                return
    
            for i in range(len(self.rev)):
                if self.rev[i] == 0 and start == 0:
                    continue
                if n != 1:
                    if self.rev[i] == 6:
                        temp[start] = 6 
                        temp[0-(start+1)] = 9
                    elif self.rev[i] == 9:
                        temp[start] = 9 
                        temp[0-(start+1)] = 6
                    else:
                        temp[start] = self.rev[i]
                        temp[0-(start+1)] = self.rev[i]
                    self.helper(n-2, start+1, temp)
                    #temp[start] = None
                    #temp[-start+1] = None
                else:
                    if self.rev[i] != 6 and self.rev[i] != 9:
                        temp[start] = self.rev[i]
                        self.helper(n-1, start+1, temp)
                        #temp[start] = None

Log in to reply
 

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