《LeetCode之每日一题》:175.数字转换英文表示

Posted 是七喜呀!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《LeetCode之每日一题》:175.数字转换英文表示相关的知识,希望对你有一定的参考价值。

整数转换英文表示


题目链接: 整数转换英文表示

有关题目

将非负整数 num 转换为其对应的英文表示。
示例 1:

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

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

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

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

0 <= num <= 2^31 - 1

题解

法一:递归
参考官方题解

class Solution {
public:
    vector<string> singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    vector<string> teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    vector<string> tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    vector<string> thousands = {"", "Thousand", "Million", "Billion"};
    
    string numberToWords(int num) {
        //特判 0
        if (num == 0)
            return "Zero";

        string ans;//2,147,483,647
        for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000)
        {
            int curNum = num / unit;
            if (curNum != 0)
            {
                num -= curNum * unit;
                string cur;
                recursion(cur, curNum);
                cur = cur + thousands[i] + " ";
                ans = ans + cur;
            }
        }
        while(ans.back() == ' ')
            ans.pop_back();
        
        return ans;
    }

    void recursion(string &curr, int curNum)
    {
        if (curNum == 0)
            return ;
        else if (curNum < 10)
        {
            curr = curr + singles[curNum] + " ";
        }
        else if (curNum < 20)
        {
            curr = curr + teens[curNum - 10] + " ";
        }
        else if (curNum < 100)
        {
            curr = curr + tens[curNum / 10] + " ";
            recursion(curr, curNum % 10);
        }
        else 
        {
            curr = curr + singles[curNum / 100] + " Hundred ";
            recursion(curr, curNum % 100);
        }
    }
};


法二:迭代
参考官方题解

class Solution {
public:
    vector<string> singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    vector<string> tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    vector<string> teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    vector<string> thousands = {"", "Thousand", "Million", "Billion"};
    string numberToWords(int num) {
        if (num == 0)
            return "Zero";
        
        string ans;
        for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000)
        {
            int curNum = num / unit;
            if (curNum != 0)
            {
                num -= curNum * unit;
                ans = ans + toEnglish(curNum) + thousands[i] + " ";
            }
        }
        while(ans.back() == ' ')
            ans.pop_back();
        
        return ans;
    }

    string toEnglish(int num)
    {
        string curr;
        int hundred = num / 100;
        num %= 100;
        if (hundred != 0)
        {
            curr = curr + singles[hundred] + " Hundred ";
        }

        int ten = num / 10;
        if (ten >= 2)
        {
            curr = curr + tens[ten] + " ";
            num %= 10;
        }
        
        //注意判断条件
        if (num > 0 && num < 10)
            curr = curr + singles[num] + " ";
        else if (num >= 10 && num < 20)
            curr = curr + teens[num % 10] + " ";

        return curr;
    }
};

以上是关于《LeetCode之每日一题》:175.数字转换英文表示的主要内容,如果未能解决你的问题,请参考以下文章

《LeetCode之每日一题》:181.数字的补数

《LeetCode之每日一题》:265.至少是其他数字两倍的最大数

《LeetCode之每日一题》:201.猜数字游戏

《LeetCode之每日一题》:61.猜数字大小

《LeetCode之每日一题》:199.丢失的数字

《LeetCode之每日一题》:122.字符串相加