Integer to English Words -- leetcode

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"

基本思路:

需要特殊處理的基本數字是,1~19,

然後就是整十,如,20,30,40,,,90

然後要注意是空格的添加。


class Solution {
public:
    string numberToWords(int num) {
        if (!num)
            return "Zero";
        return numberToEnglish(num).substr(1);
    }
    
    string numberToEnglish(int num) {
        static const char *oneTo19Map[] = 
            {"", "One", "Two", "Three", "Four",
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Eleven", "Twelve",
            "Thirteen", "Fourteen", "Fifteen", "Sixteen",
            "Seventeen", "Eighteen", "Nineteen"};
        static const char *tensMap[] = 
            {"", "", "Twenty", "Thirty", "Forty", "Fifty",
            "Sixty", "Seventy", "Eighty", "Ninety"};
        if (num >= 1000000000)
            return numberToEnglish(num / 1000000000) + " Billion" + numberToEnglish(num % 1000000000);
        else if (num >= 1000000)
            return numberToEnglish(num / 1000000) + " Million" + numberToEnglish(num % 1000000);
        else if (num >= 1000)
            return numberToEnglish(num / 1000) + " Thousand" + numberToEnglish(num % 1000);
        else if (num >= 100)
            return numberToEnglish(num / 100) + " Hundred" + numberToEnglish(num % 100);
        else if (num >= 20)
            return string(" ") + tensMap[num/10] + numberToEnglish(num % 10);
        else if (num)
            return string(" ") + oneTo19Map[num];
        else
            return "";
    }
};


最後將自己的第一版也貼上作個紀念:

class Solution {
public:
    string numberToWords(int num) {
        const char *thousandsMap[] = {"", " Thousand", " Million", " Billion"};
        int base = 1;
        int index = 0;
        while (num / base >= 1000) {
            ++index;
            base *= 1000;
        }
        string ans;
        while (base) {
            string item = numberToWordsUnit(num / base);
            if (!item.empty()) {
                if (!ans.empty())
                    ans.push_back(' ');
                ans += item + thousandsMap[index];
            }
            num %= base;
            base /= 1000;
            --index;
        }
        return ans.empty() ? "Zero" : ans;
    }
    
    string numberToWordsUnit(int num) {
        const char *OneTo19[] = 
            {"Zero", "One", "Two", "Three", "Four",
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Eleven", "Twelve",
            "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
            "Eighteen", "Nineteen"};
        const char *decadeMap[] = 
            {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
            "Sixty", "Seventy", "Eighty", "Ninety"};
        string ans;
        const int hundred = num / 100;
        num %= 100;
        if (hundred)
            ans += string(OneTo19[hundred]) + " Hundred";
        if (!num)
            return ans;
        if (!ans.empty())
            ans.push_back(' ');
        if (num < 20) {
            ans += OneTo19[num];
        }
        else {
            const int decade = num / 10;
            num %= 10;
            ans += decadeMap[decade];
            if (!num)
                return ans;
            if (!ans.empty())
                ans.push_back(' ');
            ans += OneTo19[num];
        }
        return ans;
    }
};



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章