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
相关推荐
大模型真好玩2 分钟前
深入浅出LangGraph AI Agent智能体开发教程(一)—全面认识LangGraph
人工智能·python·mcp
2501_9247311120 分钟前
智慧城市交通场景误检率↓78%!陌讯多模态融合算法实战解析
人工智能·算法·目标检测·视觉检测·智慧城市
乘乘凉2 小时前
Python中函数的闭包和装饰器
前端·数据库·python
PAK向日葵3 小时前
【算法导论】XHS 0824 笔试题解
算法·面试
2501_924534894 小时前
智慧零售商品识别误报率↓74%!陌讯多模态融合算法在自助结算场景的落地优化
大数据·人工智能·算法·计算机视觉·目标跟踪·视觉检测·零售
盖雅工场4 小时前
连锁零售排班难?自动排班系统来解决
大数据·人工智能·物联网·算法·零售
Greedy Alg4 小时前
LeetCode 438. 找到字符串中所有的字母异位词
算法·leetcode·职场和发展
Q741_1474 小时前
C++ 力扣 76.最小覆盖子串 题解 优选算法 滑动窗口 每日一题
c++·算法·leetcode·双指针·滑动窗口
爱隐身的官人7 小时前
爬虫基础学习-爬取网页项目(二)
前端·爬虫·python·学习