zoukankan      html  css  js  c++  java
  • 273. Integer to English Words

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

    Example 1:

    Input: 123
    Output: "One Hundred Twenty Three"

    Example 2:

    Input: 12345
    Output: "Twelve Thousand Three Hundred Forty Five"

    Example 3:

    Input: 1234567
    Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

    Example 4:

    Input: 1234567891
    Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

    class Solution(object):
        def numberToWords(self, num):
            """
            :type num: int
            :rtype: str
            """
            if num==0:
                return 'Zero'
            l1 = ['','One ','Two ','Three ','Four ','Five ','Six ','Seven ','Eight ','Nine ']
            l2 = ['Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen ']
            l3 = ['','','Twenty ','Thirty ','Forty ','Fifty ','Sixty ','Seventy ','Eighty ','Ninety ']
            l4 = ['','Hundred ','Thousand ','Million ','Billion ']
            s = str(num)
            def solve(n,i):
                res = ''
                if n=='000':
                    return res
                if n[0] != '0':
                    res += l1[int(n[0])] + l4[1]
                if n[1] == '1':
                    res += l2[int(n[2])%10]
                else:
                    res += l3[int(n[1])] + l1[int(n[2])]
                return res + l4[i]
            s = '0' * (12-len(s)) + s
            output = solve(s[0:3],4) + solve(s[3:6],3) + solve(s[6:9],2) + solve(s[9:12],0)
            return output.rstrip()
    

    tip:补到同样的12位方便处理

  • 相关阅读:
    前端必须掌握的 docker 技能(2)
    前端必须掌握的 docker 技能(1)
    异步 map 和模块打包
    import 和组件库按需引入
    babel-node 和 nodemon
    linux和mac 终端代理
    yarn.lock 是干什么的
    pwa 总结
    设计模式(5): vue 不监听绑定的变量
    django+celery+redis应用
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9737109.html
Copyright © 2011-2022 走看看