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
相关推荐
Kaltistss9 分钟前
98.验证二叉搜索树
算法·leetcode·职场和发展
im_AMBER10 分钟前
学习日志05 python
python·学习
知己如祭13 分钟前
图论基础(DFS、BFS、拓扑排序)
算法
大虫小呓15 分钟前
Python 处理 Excel 数据 pandas 和 openpyxl 哪家强?
python·pandas
mit6.82422 分钟前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug25 分钟前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒27 分钟前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
军训猫猫头1 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success1 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
摸爬滚打李上进1 小时前
重生学AI第十六集:线性层nn.Linear
人工智能·pytorch·python·神经网络·机器学习