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: 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"

 

解题思路:

我们可以来看一下0-1 billion之间:

        1  0  0  0  0  0  0  0  0  0

       billion        million      thousand hundred   

                  |-----------|          |-----------|        |------------|

观察可以发现:每3个0更换一个单位。

我们可以设置一个unit 的hash map,存储单位,注意0最好也存上相对应的“”

再对1-9, 10-19, 20, 30, 40, 50, 60, 70 ,80, 90进行存储

对num进行模1000操作取出当前后3位,对后三位进行数文转换后根据当前技术值count加后面的单位。

需要注意的是:

  什么时候插入空格!

代码:

class Solution {
public:
    string numberToWords(int num) {
        unordered_map<int,string> dict ={
            {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"}
        };
        unordered_map<int,string> unit = {{0, ""},{1, " Thousand"}, {2, " Million"}, {3," Billion"}};
        string ret = "";
        int count = 0;
        if(num == 0)
            return "Zero";
        while(num > 0){
            int cur = num % 1000;
            if(cur != 0){
                string wn = toWords(cur, dict);
                wn += unit[count];
                if(ret.size() != 0){
                    ret = " "+ret;
                }
                ret = wn + ret;
            }
            count++;
            num /= 1000;
        }
        return ret;
        
    }
private:
    string toWords(int n, unordered_map<int, string> &m){
        string ret;
        int h = n/100;
        if(h != 0)
            ret = m[h] + " Hundred";
        n %= 100;
        if(n == 0)
            return ret;
        if(m.count(n)){
            if(ret.size() != 0)
                ret += " ";
            ret += m[n];
        }else{
            int t = n/10;
            if(t != 0){
                if(ret.size() != 0)
                    ret += " ";
                ret += m[t*10];
            }
            int d = n%10;
            if(d != 0){
                if(ret.size() != 0)
                    ret += " ";
                ret += m[d];
            }
        }
        return ret;
    }
};

 存一个看起来很有条理的实现

class Solution {
private:
        vector<string> wordForSingleDigit = {
            "Zero",
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
            "Eleven",
            "Twelve",
            "Thirteen",
            "Fourteen",
            "Fifteen",
            "Sixteen",
            "Seventeen",
            "Eighteen",
            "Nineteen",
            "Twenty"
        };
        
        vector<string> wordForDigitOfTen = {
            "",
            "Ten",
            "Twenty",
            "Thirty",
            "Forty",
            "Fifty",
            "Sixty",
            "Seventy",
            "Eighty",
            "Ninety"
        };
        
public:
    string numberToWords(int num) {
        if (num == 0) return "Zero";
        
        vector<string> lionText = {
            "",
            "Thousand",
            "Million",
            "Billion",
            "Trillion",
        };
        
        
        string result = "";
        int count = 0;
        
        while (num > 0) {
            int d = num % 1000;
            
            string s = englishNumberToText3(d);
            
            if (s.length() > 0)
                result = s + " " + lionText[count] + " " + result;
            
            num = num / 1000;
            
            count++;
        }
        
        while (result[result.length() - 1] == ' ')
            result = result.substr(0, result.length() - 1);

        return result;
    }
    
    string englishNumberToText3(int value) {
        if (value == 0) return "";
        
        string result = "";
        int lastTwoDigits = value % 100;
        if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
            result = wordForSingleDigit[lastTwoDigits];
            value = value - lastTwoDigits;
        }
        
        int digit0 = value % 10;
        int digit1 = (value / 10) % 10;
        int digit2 = (value / 100) % 10;
        
        if (digit0 > 0)
            result = wordForSingleDigit[digit0] + " " + result;

        if (digit1 > 0)
            result = wordForDigitOfTen[digit1] + " " + result;

        if (digit2 > 0)
            result = wordForSingleDigit[digit2] + " Hundred " + result;
        
        while (result[result.length() - 1] == ' ')
            result = result.substr(0, result.length() - 1);
        
        return result;
    }
};

 

posted @ 2018-06-17 07:32  妖域大都督  阅读(135)  评论(0编辑  收藏  举报