其他分享
首页 > 其他分享> > 273. 整数转换英文表示

273. 整数转换英文表示

作者:互联网

273. 整数转换英文表示

方法一:递归

由于非负整数 num 的最大值为 2 31 − 1 2^{31}-1 231−1,因此最多有 10 位数。将整数转换成英文表示中,将数字按照 3 位一组划分,将每一组的英文表示拼接之后即可得到整数 num 的英文表示。

每一组最多有 3 位数,即数字表示是每三位一组进行的,可以使用递归的方式得到每一组的英文表示。根据数字所在的范围,具体做法如下:

从高到低的每一组的单位依次是 1 0 9 10^9 109 、 1 0 6 10^6 106 、 1 0 3 10^3 103 、1,除了最低组以外,每一组都有对应的表示单位的词,分别是 “Billion"、“Million"、“Thousand"。

得到每一组的英文表示后,需要对每一组加上对应的表示单位的词,然后拼接得到整数 num 的英文表示。

具体实现中需要注意以下两点:

只有非零的组的英文表示才会拼接到整数 num 的英文表示中;

如果 num=0,则不适用上述做法,而是直接返回 “Zero"。

singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
thousands = ["", "Thousand", "Million", "Billion"]

class Solution:
    def numberToWords(self, num: int) -> str:
        if num == 0:
            return "Zero"

        def recursion(num: int) -> str:
            s = ""
            if num == 0:
                return s
            elif num < 10:
                s += singles[num] + " "
            elif num < 20:
                s += teens[num - 10] + " "
            elif num < 100:
                s += tens[num // 10] + " " + recursion(num % 10)
            else:
                s += singles[num // 100] + " Hundred " + recursion(num % 100)
            return s

        s = ""
        unit = int(1e9)
        for i in range(3, -1, -1):
            curNum = num // unit
            if curNum:
                num -= curNum * unit
                s += recursion(curNum) + thousands[i] + " "
            unit //= 1000
        return s.strip()

方法二:迭代

也可以使用迭代的方式得到每一组的英文表示。由于每一组最多有 3 位数,因此依次得到百位、十位、个位上的数字,生成该组的英文表示,注意只有非零位才会被添加到英文表示中。

singles = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
thousands = ["", "Thousand", "Million", "Billion"]

class Solution:
    def numberToWords(self, num: int) -> str:
        if num == 0:
            return "Zero"

        def toEnglish(num: int) -> str:
            s = ""
            if num >= 100:
                s += singles[num // 100] + " Hundred "
                num %= 100
            if num >= 20:
                s += tens[num // 10] + " "
                num %= 10
            if 0 < num < 10:
                s += singles[num] + " "
            elif num >= 10:
                s += teens[num - 10] + " "
            return s

        s = ""
        unit = int(1e9)
        for i in range(3, -1, -1):
            curNum = num // unit
            if curNum:
                num -= curNum * unit
                s += toEnglish(curNum) + thousands[i] + " "
            unit //= 1000
        return s.strip()
class Solution:
    def __init__(self):
        self.nt = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
        "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
        self.tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
        self.t = ["Thousand", "Million", "Billion"]

    def numberToWords(self, num: int) -> str:
        def helper(num) -> list[str]:
            if num < 20:
                return [self.nt[num]]
            elif num < 100:
                res = [self.tens[num//10]]
                if num % 10:
                    res += helper(num % 10)
                return res
            elif num < 1000:
                res = [self.nt[num//100], "Hundred"]
                if num % 100:
                    res += helper(num%100)
                return res
            for p, w in enumerate(self.t, 1):
                if num < 1000 ** (p + 1):
                    return helper(num // 1000 ** p) + [w] + helper(num % 1000 ** p) if num % 1000 ** p else helper(num // 1000 ** p) + [w]
        return " ".join(helper(num))

标签:10,return,self,num,273,整数,英文,100
来源: https://blog.csdn.net/weixin_43955170/article/details/120696715