• Leetcode 273. Integer to English Words


    Problem

    Convert a non-negative integer num to its English words representation.

    Algorithm

    Three consecutive digit make one group. Simulation processing will suffice and specifically deal the number less than 20.

    Code

    class Solution:
        def numberToWords(self, num: int) -> str:
    
            def units(num):
                if    1 == num: return "One"
                elif  2 == num: return "Two"
                elif  3 == num: return "Three"
                elif  4 == num: return "Four"
                elif  5 == num: return "Five"
                elif  6 == num: return "Six"
                elif  7 == num: return "Seven"
                elif  8 == num: return "Eight"
                elif  9 == num: return "Nine"
                elif 10 == num: return "Ten"
                elif 11 == num: return "Eleven"
                elif 12 == num: return "Twelve"
                elif 13 == num: return "Thirteen"
                elif 14 == num: return "Fourteen"
                elif 15 == num: return "Fifteen"
                elif 16 == num: return "Sixteen"
                elif 17 == num: return "Seventeen"
                elif 18 == num: return "Eighteen"
                elif 19 == num: return "Nineteen"
                return ""
    
            def tens(num):
                if   2 == num: return "Twenty"
                elif 3 == num: return "Thirty"
                elif 4 == num: return "Forty"
                elif 5 == num: return "Fifty"
                elif 6 == num: return "Sixty"
                elif 7 == num: return "Seventy"
                elif 8 == num: return "Eighty"
                elif 9 == num: return "Ninety"
                return ""
    
            def two_digit(num):
                if num < 20: 
                    return units(num)
                else:
                    ans = tens(num // 10)
                    num = num - num // 10 * 10
                    if num > 0:
                        ans += " " + units(num - num // 10 * 10)
                    return ans
    
            def three_digit(num):
                ans = ""
                if num >= 100:
                    ans += units(num // 100) + " Hundred"
                    num = num - num // 100 * 100
                if num > 0:
                    if ans != "": ans += " "
                    ans += two_digit(num)
                return ans
            
            if 0 == num:
                return "Zero"
    
            ans = ""
            if num >= 1000000000: 
                ans += three_digit(num // 1000000000) + " Billion"
                num = num - num // 1000000000 * 1000000000
            if num >= 1000000:
                if ans != "": ans += " "
                ans += three_digit(num // 1000000) + " Million"
                num = num - num // 1000000 * 1000000
            if num >= 1000: 
                if ans != "": ans += " "
                ans += three_digit(num // 1000) + " Thousand"
                num = num - num // 1000 * 1000
            if num > 0:
                if ans != "": ans += " "
                ans += three_digit(num)
    
            return ans
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    Qt5开发从入门到精通——第六篇四节( 图像与图片——显示SVG格式图片 )
    火山引擎 ByteHouse:ClickHouse 如何保证海量数据一致性
    面试题——git
    1.python简介
    【Git】git的安装与使用教程
    FPGA的主流技术与市场表现方面的调研报告
    【MySQL】左连接右连接内连接与Hash连接、子查询原理与实战(MySQL专栏启动)
    Raft协议浅析
    热门开源项目推荐
    4---Linux:gcc,g++编译/制作并调用静态库,动态库/makefile
  • 原文地址:https://blog.csdn.net/mobius_strip/article/details/132955040