C++ solution 4ms


  • 11
    P

    //8ms

    string helper(int& num){
    		const static char* less_ten[] =
    		{ "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine" };
    		const static char* less_twenty[] =
    		{ " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
    		const static char* less_hundred[] =
    		{ "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
    
    		int less_thousand = num % 1000;
    		num /= 1000;
    		string s; 
    		
    		if (less_thousand != 0){
    			int hundred = less_thousand / 100;
    			less_thousand %= 100; 
    			int tenth = less_thousand / 10; 
    			int single = less_thousand % 10; 
    			
    			if (hundred) s = s + less_ten[hundred] + " Hundred";
    			
    			if (tenth){
    				if (tenth == 1){
    		            s += less_twenty[single];
    					return s;
    				}
    				else  s += less_hundred[tenth];
    
    			}
    			if (single) s += less_ten[single];
    		}
    		return s;
    	}
    	string numberToWords(int num) {
    		const static char* unit[] = 
    		    { "", " Thousand", " Million", " Billion", " Triliion" };
    		    
    		string s;
    		int i = 0;
    		while (num){
    			string part = helper(num);
    			if(i++ == 0){
    			    s = part; 
    			} 
    			else if (part.size()) s = part + unit[i] + s;
    		}
    		s = s.size() ? s.substr(1) : "Zero";
    		return s;
    	}
    

    A faster version and maybe easier to understand (4ms):

    string helper(int num){
    	const static char* less_ten[] =
    	{ "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine" };
    	const static char* less_twenty[] =
    	{ " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
    	const static char* less_hundred[] =
    	{ "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
    
    	string s; 
    	
    	if (num != 0){
    	    //get hundredth, tenth, and single digit
    		int hundred = num / 100;
    		num %= 100; 
    		int tenth = num / 10; 
    		int single = num % 10; 
    		
    		if (hundred) s = s + less_ten[hundred] + " Hundred";
    		
    		if (tenth){
    			if (tenth == 1){ //special handling, choose from less_twenty based on value of single
    	            s += less_twenty[single];
    				return s;
    			}
    			else  s += less_hundred[tenth];
    
    		}
    		if (single) s += less_ten[single];
    	}
    	return s;
    }
    string numberToWords(int num) {
    	const static char* unit[] = 
    	    { "", " Thousand", " Million", " Billion" };
    	int parts[4] = {0};
    	for(int i  = 0; i < 4; ++i){
    	    parts[i] = num % 1000;
    	    num /= 1000; 
    	}
    	string s; 
    	for(int i = 0; i < 4; ++i){
    	    if(parts[i] == 0) continue; 
    	    s = helper(parts[i]) + unit[i] + s;  
    	}
    	s = s.size() ? s.substr(1) : "Zero";
    	return s;
    }

  • 0
    0

    Great solution!


  • 0

    Very clever idea to break num into hundred, tenth and single :-) BTW, you seem to have misspelled trillion. But, anyway, the test cases do not contain such large numbers as they have surpassed 2 ^ 31 - 1 :-)


  • 0
    P

    Thanks for pointing out. Correct it.


Log in to reply
 

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