leetcode246-- Strobogrammatic Number I (Locked)

leetcode246-- Strobogrammatic Number I (Locked)

题目

即判断一个数是不是反过来之后还和以前是一样的

解法如下,提供了两种解法,一种是直接判断,一种是先建立一个字典


def isStrobogrammatic(num):

    num = str(num)
    left, right = 0, len(num)-1

    while (left<=right):
        if num[left]==num[right]:
            if num[left] != "1" and num[left] !="0" and num[left] != "8":
                return False

        else:
            if (num[left] !="6" or num[right] != "9") and (num[left] != "9" or num[right] != "6"):
                return False

        left += 1
        right -= 1



    return True



import sys

ss = int(sys.argv[1])

print(ss, isStrobogrammatic(ss))


## 法二,建立一个字典

dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}

def isStrobogrammatic2(num):
    num = str(num)

    for i in range(len(num)//2+1):

        if num[i] in dic.keys():
            if dic[num[i]] != num[len(num)-1-i]:
                return False
        else:
            return False


    return True


print(ss, isStrobogrammatic2(ss))


打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦