LeetCode 273. Integer to English Words

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

v2-473b8c208860d57633a5dd629ff71c07_1440w.jpg

Description



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"


描述



将非负整数转换为其对应的英文表示。可以保证给定输入小于 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"


思路



  • 数字三个为一节,我们每三个数字做一个分段,对其用用英文表达出来,在加上对应节所在的单位即可.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-04 19:25:43
# @Last Modified by:   何睿
# @Last Modified time: 2019-02-04 20:31:32
class Solution:
    def numberToWords(self, num):
        """
        :type num: int
        :rtype: str
        """
        # 维护一个数字到英语的转换字典
        self.IntEng = {
            "0": 'Zero',
            "1": 'One',
            "2": 'Two',
            "3": 'Three',
            "4": 'Four',
            "5": 'Five',
            "6": 'Six',
            "7": 'Seven',
            "8": 'Eight',
            "9": 'Nine',
            "10": 'Ten',
            "11": 'Eleven',
            "12": 'Twelve',
            "13": 'Thirteen',
            "14": 'Fourteen',
            "15": 'Fifteen',
            "16": 'Sixteen',
            "17": 'Seventeen',
            "18": 'Eighteen',
            "19": 'Nineteen',
            "20": 'Twenty',
            "30": 'Thirty',
            "40": 'Forty',
            "50": 'Fifty',
            "60": 'Sixty',
            "70": 'Seventy',
            "80": 'Eighty',
            "90": 'Ninety',
            "100": 'Hundred',
        }
        # 没三节为一个单位,2^31最大用到的单位为Billion
        units = ['', 'Thousand', 'Million', 'Billion']
        res = ''
        # 将数字转换成为字符串
        _str = str(num)
        # count:字符串中字符个数,i:数字三个为一节,当前数字所在节
        count, i = len(_str), 0
        while count - (i + 1) * 3 >= 0:
            # 取出三个数字
            _num = _str[count - (i + 1) * 3:count - i * 3]
            # 三个数字都不为0进行计算
            if not _num == "000":
                s = self.__readthree(_num)
                res = s + " " + units[i] + " " + res
            # 进入下一节
            i += 1
        # 如果最后一节不足三个数,处理剩下的数
        if count - (i * 3):
            s = self.__readthree(_str[0:count - (i * 3)])
            res = s + " " + units[i] + " " + res
        # 返回结果,去掉最后的空格
        return res.strip()
    # 私有函数,读一节(三个数字)数
    def __readthree(self, _str):
        _str = str(int(_str))
        if len(_str) == 1:
            return self.IntEng[_str]
        if len(_str) == 2:
            return self.__readtwo(_str)
        if len(_str) == 3:
            res = self.IntEng[_str[0]] + " " + "Hundred"
            if int(_str) % 100:
                return res + " " + self.__readtwo(_str[1:])
            else:
                return res
    # 私有函数,读两位数,辅助读一节数字
    def __readtwo(self, _str):
        _str = str(int(_str))
        if int(_str) <= 20 or _str[1] == "0":
            return self.IntEng[_str]
        else:
            return self.IntEng[_str[0] + '0'] + " " + self.IntEng[_str[1]]


源代码文件在这里.


目录
相关文章
|
索引 Python
LeetCode 820. 单词的压缩编码 Short Encoding of Words
LeetCode 820. 单词的压缩编码 Short Encoding of Words
|
Java 索引 Python
LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
1596 0