Python solution with dictionary.


  • 9
    C
    def isStrobogrammatic1(self, num):
        deque = collections.deque(map(int, list(num)))
        while len(deque) >= 2:
            l, r = deque.popleft(), deque.pop()
            for i in [2,3,4,5,7]:
                if i in [l, r]:
                    return False  
            if (l, r) in [(6,6), (9,9)] or (l != r and (l, r) not in [(6,9), (9,6)]):
                return False
        return not deque or deque.pop() in [0,1,8]
        
    def isStrobogrammatic(self, num):
        dic = {"0":"0", "1":"1", "6":"9", "8":"8", "9":"6"}
        l, r = 0, len(num)-1
        while l <= r:
            if num[l] not in dic or dic[num[l]] != num[r]:
                return False
            l += 1
            r -= 1
        return True

  • 2
    W

    My code based on your idea:

    class Solution(object):
        def isStrobogrammatic(self, num):
            """
            :type num: str
            :rtype: bool
            """
            
            dic = {"0":"0", "1":"1", "6":"9", "8":"8", "9":"6"}
            res = []
            for d in list(num):
                if d in dic:
                    res.append(dic[d])
                    
                else:
                    return False
            return list(num) == res[::-1]

Log in to reply
 

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