美文网首页
273. Integer to English Words [H

273. Integer to English Words [H

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-06-01 21:11 被阅读0次
    273. Integer to English Words
    class Solution(object):
        def numberToWords(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num==0:
                return "Zero"
            vocab = {0:"", 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 ",
                     20:"Twenty ", 30:"Thirty ", 40:"Forty ", 50:"Fifty ",
                     60:"Sixty ", 70:"Seventy ", 80:"Eighty ", 90:"Ninety ",
                     100:"Hundred ", 1000:"Thousand ", 1000000:"Million ",
                     1000000000:"Billion ", 1000000000000:"Trillion ",
                     1000000000000000:"Quadrillion"}
            cnt = 1
            res = ''
            while num:
                temp = ''
                len3 = num % 1000
                num = num // 1000
                index1 = len3 // 100
                if index1 != 0:
                    temp = temp + vocab[index1] + vocab[100]
                len2 = len3 % 100
                if len2 < 20:
                    temp = temp + vocab[len2]
                else:
                    a = len2 % 10
                    b = len2 - a
                    temp = temp + vocab[b] + vocab[a]
                if cnt == 1:
                    add = ''
                else:
                    add = vocab[cnt]
                if temp:
                    temp = temp + add
                cnt *= 1000
                res = temp + res
            return res.strip()
    

    相关文章

      网友评论

          本文标题:273. Integer to English Words [H

          本文链接:https://www.haomeiwen.com/subject/ygqztctx.html