My Java Solution


  • 14
    W
    public String numberToWords(int num) {
        if(num == 0)
            return "Zero";
        String[] bigString = new String[]{"Thousand","Million","Billion"};
        String result =  numberToWordsHelper(num%1000);
        num = num/1000;
        if(num > 0 && num%1000>0){
            result = numberToWordsHelper(num%1000) + "Thousand " + result;
        }
        num = num/1000;
        if(num > 0 && num%1000>0){
            result = numberToWordsHelper(num%1000) + "Million " + result;
        }
        num = num/1000;
        if(num > 0){
            result = numberToWordsHelper(num%1000) + "Billion " + result;
        }
        return result.trim();
    }
    
    public String numberToWordsHelper(int num){
        String[] digitString = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        String[] teenString = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"};
        String[] tenString = new String[]{"","","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        String result = "";
        if(num > 99){
            result += digitString[num/100] + " Hundred ";
        }
        num = num % 100;
        if(num < 20 && num > 9){
            result += teenString[num%10]+" ";
        }else{
            if(num > 19){
                result += tenString[num/10]+" ";
            }
            num = num % 10;
            if(num > 0)
                result += digitString[num]+" ";
        }
        return result;
    }

  • 0
    W

    Great idea! Can you post a version in C++?


  • 1

    Great code! Well, I notice that you define

    String[] bigString = new String[]{"Thousand","Million","Billion"};
    

    I guess you want to use this to compress the three appearances of the following codes into a loop to avoid writing duplicate codes?

    num = num/1000;
    if(num > 0 && num%1000>0){
        result = numberToWordsHelper(num%1000) + "Thousand " + result;
    }
    

    Well, I rewrite your code in C++. Since C++ has no convenient functions like trim and I do not want to create one on my own, I handle the " " and ' ' carefully using some tricks from this link (for example, I include the space in the words of the numbers).

    The code is as follows, in which I organize hundred/thousand/million into a loop.

    class Solution {
    public: 
    	string numberToWords(int num) {
    		vector<string> bigs = { "", " Thousand", " Million", " Billion" };
    		int i = 0;
    		string ans;
    		while (num) {
    			if (num % 1000) ans = numberWords(num % 1000) + bigs[i] + ans;
    			i++, num /= 1000;
    		}
    		return ans.length() ? ans.substr(1) : "Zero";
    	}
    private:
    	string numberWords(int num) {
    		char* one[] = { "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine" };
    		char* ones[] = { " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
    		char* tens[] = { "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
    		string ans;
    		if (num > 99) ans += string(one[num / 100]) + " Hundred";
    		num %= 100;
    		if (num > 9 && num < 20) ans += ones[num % 10];
    		else {
    			if (num > 19) ans += tens[num / 10];
    			num %= 10;
    			if (num) ans += one[num];
    		}
    		return ans;
    	}
    };
    

  • 0

    Hi, I rewrite the code in C++ above. Well, since C++ has no convenient functions like the Java trim, some tricks are needed to handle redundant spaces.


Log in to reply
 

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