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

python3 复制代码
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
相关推荐
u0110225122 分钟前
如何自定义查询历史记录面板的展示风格_时间轴样式设计
jvm·数据库·python
2301_769340674 分钟前
HTML怎么实现快捷跳转顶部_HTML固定悬浮锚点按钮【介绍】
jvm·数据库·python
夏日听雨眠6 分钟前
数据结构(循环队列)
数据结构·算法·链表
平行侠11 分钟前
30MacLaren-Marsaglia算法故事文件
数据结构·算法
yuanpan13 分钟前
Python + PyAutoGUI 实战:Windows 自动化办公脚本开发入门
windows·python·自动化
m0_6091604916 分钟前
MySQL如何限制触发器递归调用的深度_防止触发器死循环方法
jvm·数据库·python
灵动小溪19 分钟前
claude code工具PC安装部署
人工智能·算法
zjy2777723 分钟前
Golang bcrypt如何加密密码_Golang密码加密教程【收藏】
jvm·数据库·python
老纪37 分钟前
Redis怎样利用Lua为多个Key同步续期
jvm·数据库·python
2403_8832610940 分钟前
C#怎么计算两个日期的差值_C#如何处理时间跨度【笔记】
jvm·数据库·python