[LeetCode]47. Integer to English Words整数的读法

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

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

解法:按billion、million、thousand和hundred及以下依次处理即可。

class Solution {
public:
    string numberToWords(int num) {
        vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
        vector<string> dec1 = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
        vector<string> dec2 = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        vector<string> expr = { "Thousand", "Million", "Billion" };
        string res = "";
        for (int i = 9; i >= 0; i -= 3)
        {
            int div = num / (int)pow(10, i);
            if (div > 0 && div < 10)
            {
                res += unit[div - 1] + " ";
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            else if (div >= 10 && div < 20)
            {
                res += dec1[div - 10] + " ";
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            else if (div >= 20)
            {
                if (div >= 20 && div < 100)
                {
                    int bas = div / 10, reu = div % 10;
                    res += dec2[bas] + " ";
                    if (reu > 0) res += unit[reu - 1] + " ";
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
                else if (div >= 100)
                {
                    int hun = div / 100, tne = div / 10 % 10, uni = div % 10;
                    res += unit[hun - 1] + " Hundred ";
                    if (tne == 1) res += dec1[div % 100 - 10] + " ";
                    else if (tne > 1 || uni > 0)
                    {
                        if(tne > 1) res += dec2[tne] + " ";
                        if (uni > 0) res += unit[uni - 1] + " ";
                    }
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
            }
            num %= (int)pow(10, i);
            if (num <= 0) break;
        }
        if (res.empty()) res += "Zero ";
        res.resize(res.size() - 1);
        return res;
    }
};

改进:将输入整数用billion、million、thousand除后,所得的商均在0-999之间,因此可以将0-999之间的读法单独提出来,作为一个工具函数。

class Solution {
public:
    string numberToWords(int num) {
        if (num == 0) return "Zero";
        vector<string> expr = { "Thousand", "Million", "Billion" };
        string res = "";
        for (int i = 9; i >= 0; i -= 3)
        {
            int div = num / (int)pow(10, i);
            if (div > 0)
            {
                res += lessThanThousand(div);
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            num %= (int)pow(10, i);
        }
        res.resize(res.size() - 1);
        return res;
    }
private:
    string lessThanThousand(int num)
    {
        vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
        vector<string> decd = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        string res = "";
        int hun = num / 100, ten = num / 10 % 10, uni = num % 10;
        if (hun > 0) res += unit[hun - 1] + " Hundred ";
        if (ten == 1) res += decd[ten * 10 + uni - 10] + " ";
        else if (ten > 1 || uni > 0)
        {
            if (ten > 1) res += decd[8 + ten] + " ";
            if (uni > 0) res += unit[uni - 1] + " ";
        }
        return res;
    }
};
时间: 2024-07-29 09:44:28

[LeetCode]47. Integer to English Words整数的读法的相关文章

LeetCode:Integer to English Words - 按英语读法输出数字对应单词

1.题目名称 Integer to English Words(按英语读法输出数字对应单词) 2.题目地址 https://leetcode.com/problems/integer-to-english-words/ 3.题目内容 英文:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 中文:给出一个非负整数

[leetcode]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: 123Output: "One Hundred Twenty Three"Example 2: Input: 12345Output: "Twelve Thousand Three Hundred Fort

leetCode 273. Integer to English Words 字符串 | Hard

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. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundre

[LeetCode] Integer to English Words 整数转为英文单词

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g

LeetCode 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. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

[LeetCode 273] Integer to English Word

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g

273 Integer to English Words 整数转换英文表示

将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的.示例:123 -> "One Hundred Twenty Three"12345 -> "Twelve Thousand Three Hundred Forty Five"1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 详见:https

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign