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
相关推荐
WBluuue18 小时前
AtCoder Beginner Contest 438(ABCDEF)
c++·算法
天才测试猿18 小时前
Selenium测试框架快速搭建详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
玄同76518 小时前
我是如何开发项目的?——从 “踩坑思维” 到 “工程化能力”:编程学习的进阶方法论(万字版)
开发语言·人工智能·经验分享·笔记·python·学习·课程设计
Murphy_3118 小时前
从根上了解一下复指数
算法
Run_Teenage18 小时前
Linux:理解IO,重定向
linux·运维·算法
深蓝海拓18 小时前
PySide6之QListWidget 学习
笔记·python·qt·学习·pyqt
你撅嘴真丑18 小时前
素数对 与 不吉利日期
算法
多米Domi01118 小时前
0x3f 第20天 三更24-32 hot100子串
java·python·算法·leetcode·动态规划
数据光子18 小时前
【YOLO数据集】船舶检测
人工智能·python·yolo·目标检测·计算机视觉·目标跟踪
wzfj1234518 小时前
Opaque Pointer / Incomplete Type
c++·算法·c