LeetCode-Python-273. 整数转换英文表示

644 篇文章 23 订阅

将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。

示例 1:

输入: 123
输出: "One Hundred Twenty Three"
示例 2:

输入: 12345
输出: "Twelve Thousand Three Hundred Forty Five"
示例 3:

输入: 1234567
输出: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:

输入: 1234567891
输出: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/integer-to-english-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

根据英文的表达习惯,数字被分为三个三个一组,一般以逗号隔开,比如1,000,000,000。

所以不难发现本题的核心目标就是,将1000以下的整数转换成英文的代码写出来,然后将数字每三个一组,不断重复调用转换代码,最后将答案拼接在一起即可。

难不是很难,但是特别繁琐,注意英语拼写,注意edge case。

class Solution(object):
    def numberToWords(self, num):
        """
        :type num: int
        :rtype: str
        """
        def helper(num): #本函数用于处理1000 以下的整数转英文
            n = int(num)
            num = str(n)
            if n < 100:
                return subhelper(num)
            else:
                return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred " + subhelper(num[1:]) if num[1:] != "00" else ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred"
            
        def subhelper(num): #本函数用于处理100 以下的整数转英文
            n = int(num)
            l1 = ["Zero", "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"]
            if n < 10:
                return l1[int(num)]
            if 10 <= n < 20:
                return l2[n - 10]
            if 20 <= n < 100:
                return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2]

        res = ""
        if num >= 1000000000:
            res = helper(str(num)[0]) + " Billion"
            if str(num)[1:4] != "000":
                res += " " + helper(str(num)[1:4]) + " Million"
            if str(num)[4:7] != "000":
                res += " " + helper(str(num)[4:7]) + " Thousand"
            if str(num)[7:] != "000":
                res += " " + helper(str(num)[7:])
        elif num >= 1000000:
            res = helper(str(num)[:-6]) + " Million"
            if str(num)[-6:-3] != "000":
                res += " " + helper(str(num)[-6:-3]) + " Thousand"
            if str(num)[-3:] != "000":
                res += " " + helper(str(num)[-3:])
        elif num >= 1000:
            res = helper(str(num)[:-3]) + " Thousand"
            if str(num)[-3:] != "000":
                res += " " + helper(str(num)[-3:])
        else:
            return helper(str(num))
            
        return res

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值