Simple idea, tedious typing, and you need to be careful for details


  • 0
    T
    class Solution {
    private:
        string singleDigit(int num)
        {
            if(num == 0)
                return "";
            else if(num == 1)
                return "One";
            else if(num == 2)
                return "Two";
            else if(num == 3)
                return "Three";
            else if(num == 4)
                return "Four";
            else if(num == 5)
                return "Five";
            else if(num == 6)
                return "Six";
            else if(num == 7)
                return "Seven";
            else if(num == 8)
                return "Eight";
            else if(num == 9)
                return "Nine";
        }
        
        string twoDigit(int num)
        {
            if(num < 10)
                return singleDigit(num);
            else if(num == 10)
                return "Ten";
            else if(num == 11)
                return "Eleven";
            else if(num == 12)
                return "Twelve";
            else if(num == 13)
                return "Thirteen";
            else if(num == 14)
                return "Fourteen";
            else if(num == 15)
                return "Fifteen";
            else if(num == 16)
                return "Sixteen";
            else if(num == 17)
                return "Seventeen";
            else if(num == 18)
                return "Eighteen";
            else if(num == 19)
                return "Nineteen";
            else if(num == 20)
                return "Twenty";
            else if(num == 30)
                return "Thirty";
            else if(num == 40)
                return "Forty";
            else if(num == 50)
                return "Fifty";
            else if(num == 60)
                return "Sixty";
            else if(num == 70)
                return "Seventy";
            else if(num == 80)
                return "Eighty";
            else if(num == 90)
                return "Ninety";
            else if(num>20 && num < 30)
                return "Twenty " + singleDigit(num%10);
            else if(num > 30 && num < 40)
                return "Thirty " + singleDigit(num%10);
            else if(num > 40 && num < 50)
                return "Forty " + singleDigit(num%10);
            else if(num > 50 && num < 60)
                return "Fifty " + singleDigit(num%10);
            else if(num > 60 && num < 70)
                return "Sixty " + singleDigit(num%10);
            else if(num > 70 && num < 80)
                return "Seventy " + singleDigit(num%10);
            else if(num > 80 && num <90)
                return "Eighty " + singleDigit(num%10);
            else if(num > 90 && num < 100)
                return "Ninety " + singleDigit(num%10);
        }
        
        string threeDigit(int num)
        {
            if(num<100)
                return twoDigit(num);
            else if(num %100 == 0)
                return singleDigit(num/100) + " Hundred";
            else if(num>100 && num<1000 && num %100 !=0)
                return singleDigit(num/100) + " Hundred " + twoDigit(num%100);
        }
        
    public:
        string numberToWords(int num) {
            if(num == 0)
                return "Zero";
            int billion = num/(1000000000);
            int million = (num - billion*1000000000)/1000000;
            int thousand = (num - billion*1000000000 - 1000000*million)/1000;
            int rest = num - billion*1000000000 - 1000000*million - 1000*thousand;
            string s;
            if(billion > 0 && num%1000000000 == 0)
            {
                s = s + threeDigit(billion) + " Billion";
                return s;
            }
            else if(billion > 0)
                s = s + threeDigit(billion) + " Billion ";
            if(million > 0 && num%1000000 == 0)
            {
                s = s + threeDigit(million) + " Million";
            return s;
            }
            else if(million > 0)
                s = s + threeDigit(million) + " Million ";
            if (thousand > 0 && num%1000 ==0 )
            {
                s = s + threeDigit(thousand) + " Thousand";
                return s;
            }
            else if(thousand > 0)
                s = s + threeDigit(thousand) + " Thousand ";
            if (rest > 0)
                s = s + threeDigit(rest);
            
            return s;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.