mq021.整数转换英文表示

 

题目链接

这个题好像没啥好多说的,很多边界条件。1是我写的,2是我复制的官方题解,2稍微快一点应该是因为没做int到str的转化,我反正做了省得要用1000取模和余数之类的。这个算法的时间复杂度是O(N),空间复杂度是O(1)。

class Solution:
    def numberToWords1(self, num: int) -> str:
        if num == 0:
            return "Zero"
        
        snum = str(num)
        length = len(snum)
        while length % 3 != 0:
            snum = "0" + snum
            length += 1

        seperated = []
        i = 1
        while 3*i <= length:
            seperated.append(snum[3*i-3:3*i])
            i += 1

        dic1 = {1:"Hundred", 2:"Thousand", 3:"Million", 4:"Billion"}
        dic2 = {
            "1": "One", "2": "Two", "3": "Three", "4": "Four", "5": "Five",
            "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", "10": "Ten",
            "11": "Eleven", "12": "Twelve", "13": "Thirteen", "14": "Fourteen", "15": "Fifteen",
            "16": "Sixteen", "17": "Seventeen", "18": "Eighteen", "19": "Nineteen"
            }
        dic3 = {
            "2": 'Twenty', "3": 'Thirty', "4": 'Forty', "5": 'Fifty',
            "6": 'Sixty', "7": 'Seventy', "8": 'Eighty', "9": 'Ninety'
            }
        
        res = ""
        seperated = seperated[::-1]
        for j, seg in enumerate(seperated):
            print(j, seg)
            if seg == "000":
                continue
            tmp = []
            
            if seg[0] != "0":
                first_pos = dic2[seg[0]]
                tmp.append(first_pos)
                tmp.append("Hundred")
            if seg[1] == "0":
                if seg[2] != "0":
                    tmp.append(dic2[seg[2]]) 
                else:
                    pass
            elif seg[1] == "1":
                tmp.append(dic2[seg[1:]])
            else:
                tmp.append(dic3[seg[1]])
                if seg[2] != "0":
                    tmp.append(dic2[seg[2]])
            if j > 0:
                tmp.append(dic1[j+1])
            
            res = " ".join(tmp) + " " + res
        
        return res.strip(" ")

    def numberToWords2(self, num):
        def one(num):
            switcher = {
                1: 'One',
                2: 'Two',
                3: 'Three',
                4: 'Four',
                5: 'Five',
                6: 'Six',
                7: 'Seven',
                8: 'Eight',
                9: 'Nine'
            }
            return switcher.get(num)

        def two_less_20(num):
            switcher = {
                10: 'Ten',
                11: 'Eleven',
                12: 'Twelve',
                13: 'Thirteen',
                14: 'Fourteen',
                15: 'Fifteen',
                16: 'Sixteen',
                17: 'Seventeen',
                18: 'Eighteen',
                19: 'Nineteen'
            }
            return switcher.get(num)

        def ten(num):
            switcher = {
                2: 'Twenty',
                3: 'Thirty',
                4: 'Forty',
                5: 'Fifty',
                6: 'Sixty',
                7: 'Seventy',
                8: 'Eighty',
                9: 'Ninety'
            }
            return switcher.get(num)

        def two(num):
            if not num:
                return ''
            elif num < 10:
                return one(num)
            elif num < 20:
                return two_less_20(num)
            else:
                tenner = num // 10
                rest = num - tenner * 10
                return ten(tenner) + ' ' + one(rest) if rest else ten(tenner)

        def three(num):
            hundred = num // 100
            rest = num - hundred * 100
            if hundred and rest:
                return one(hundred) + ' Hundred ' + two(rest) 
            elif not hundred and rest: 
                return two(rest)
            elif hundred and not rest:
                return one(hundred) + ' Hundred'

        billion = num // 1000000000
        million = (num - billion * 1000000000) // 1000000
        thousand = (num - billion * 1000000000 - million * 1000000) // 1000
        rest = num - billion * 1000000000 - million * 1000000 - thousand * 1000

        if not num:
            return 'Zero'

        result = ''
        if billion:
            result = three(billion) + ' Billion'
        if million:
            result += ' ' if result else ''
            result += three(million) + ' Million'
        if thousand:
            result += ' ' if result else ''
            result += three(thousand) + ' Thousand'
        if rest:
            result += ' ' if result else ''
            result += three(rest)
        return result