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
相关推荐
Pfolg26 分钟前
Python+ffmpeg实现字幕视频合并
python·ffmpeg·开源软件
小鹿( ﹡ˆoˆ﹡ )1 小时前
探索TCP协议的奥秘:Python中的网络通信
网络·python·tcp/ip
2c237c62 小时前
[ 蓝桥 ·算法双周赛 ] 第 19 场 小白入门赛
算法·蓝桥云课·算法双周赛
猫武士水星2 小时前
微信步数C++
算法
这可就有点麻烦了2 小时前
强化学习笔记之【DDPG算法】
笔记·算法·机器学习
哪 吒2 小时前
华为OD机试 - 冠亚军排名(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od
CocoaAndYy2 小时前
ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal原理及Demo
java·jvm·算法
F_D_Z2 小时前
【Python】数据可视化之聚类图
python·信息可视化·聚类
DanCheng-studio3 小时前
毕业设计项目 大数据电影数据分析与可视化系统(源码+论文)
python·毕业设计·毕设
极客小张4 小时前
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
c语言·stm32·物联网·算法·毕业设计·课程设计·语言识别