Simple python code. Easy to understand


  • 0

    Strobogrammatic numbers are special. Reverse each character by 180 degrees if possible, then reverse, and compare with original number. For example: 986 ==> 689 ==> 986 (True)

    class Solution(object):
        def isStrobogrammatic(self, num):
            flipped = ""
            stromap = {'same': ['0','1','8']}
            for d in num:
                if d in stromap['same']:
                    flipped += d
                elif d == '6':
                    flipped += '9'
                elif d == '9':
                    flipped += '6'
                else:
                    return False
            if len(flipped) == 0:
                return False
            # Reverse flipped
            return flipped[::-1] == num
    

Log in to reply
 

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