leetcode-第14周双周赛-1271-十六进制魔术数字

 

 自己的提交:

class Solution:
    def toHexspeak(self, num: str) -> str:
        num = hex(int(num))
        num = str(num)[2:]
        res = ""
        for i in num:
            if i == "0":
                i = "O"
            if i == "1":
                i = "I"
            i = i.upper()
            if i not in {"A", "B", "C", "D", "E", "F", "I", "O"}:
                return "ERROR"
            res += i
        return res
        

另:

class Solution:
    def toHexspeak(self, num: str) -> str:
        s = hex(int(num))[2:].upper()
        def change(c):
            if c == '0':
                return 'O'
            elif c == '1':
                return 'I'
            else:
                return c
        if len(list(filter(lambda c: c not in 'ABCDEF01', s))) != 0:
            return "ERROR"
        return "".join(list(map(lambda c: change(c), s)))

猜你喜欢

转载自www.cnblogs.com/oldby/p/11969850.html