273. Integer to English Words

273. Integer to English Words

class Solution:
    def numberToWords(self, num: int) -> str:
        to19='One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve ' \
        'Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split()
        tens='Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split()
        def words(n):
            if n<20:
                return to19[n-1:n]
            if n<100:
                return [tens[n//10-2]]+words(n%10)
            if n<1000:
                return [to19[n//100-1]]+['Hundred']+words(n%100)
            for p,w in enumerate(('Thousand','Million','Billion'),1):
                # print(p,w)
                if n<1000**(p+1):
                    return words(n//(1000**p))+[w]+words(n%(1000**p))
        return ' '.join(words(num)) or 'Zero'

模拟题

for p,w in enumerate(('Thousand','Million','Billion'),1):

p从1开始

相关推荐

  1. ctfshow web入门 php反序列化 web275--web278(无web276

    2024-01-09 08:04:01       32 阅读
  2. 273. Integer to English Words

    2024-01-09 08:04:01       58 阅读
  3. UVA-213

    2024-01-09 08:04:01       55 阅读
  4. Leetcode274

    2024-01-09 08:04:01       31 阅读
  5. Leetcode.2735 收集巧克力

    2024-01-09 08:04:01       81 阅读
  6. 数组|274. H 指数

    2024-01-09 08:04:01       63 阅读
  7. Python comp233

    2024-01-09 08:04:01       57 阅读
  8. 213. 打家劫舍 II

    2024-01-09 08:04:01       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 08:04:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 08:04:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 08:04:01       82 阅读
  4. Python语言-面向对象

    2024-01-09 08:04:01       91 阅读

热门阅读

  1. 【Python机器学习】理论知识:决策树

    2024-01-09 08:04:01       54 阅读
  2. wiki 爬虫记录

    2024-01-09 08:04:01       59 阅读
  3. Qt Creator 常用快捷键

    2024-01-09 08:04:01       55 阅读
  4. python 人脸检测与人脸识别

    2024-01-09 08:04:01       49 阅读
  5. ctypes实现numpy和OpenCV Mat之间的数据交互

    2024-01-09 08:04:01       60 阅读
  6. HarmonyOS应用开发者基础(初级)认证题库

    2024-01-09 08:04:01       56 阅读
  7. Python处理音频文件两个非常重要库

    2024-01-09 08:04:01       61 阅读
  8. Linux 软件安装以及管理

    2024-01-09 08:04:01       59 阅读
  9. 04MyBatis核心配置文件

    2024-01-09 08:04:01       58 阅读
  10. facebook可以去批量私信吗

    2024-01-09 08:04:01       63 阅读