easy to understand c++ code


  • 1
    A
    class Solution{
    public:
    
    	Solution()
    	{
    		string d[] = { "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
    			"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
    
    		for (int i = 0; i < sizeof(d) / sizeof(string); ++i)
    		{
    			dict[i] = d[i];
    		}
    
    		dict[20] = "Twenty";
    		dict[30] = "Thirty";
    		dict[40] = "Forty";
    		dict[50] = "Fifty";
    		dict[60] = "Sixty";
    		dict[70] = "Seventy";
    		dict[80] = "Eighty";
    		dict[90] = "Ninety";
    		dict[100] = "Hundred";
    		dict[100] = "Hundred";
    		dict[1000] = "Thousand";
    		dict[1000000] = "Million";
    		dict[1000000000] = "Billion";
    	}
    	
    	string handle(int num)//less than 100
    	{		
    		string ret;
    		if (num == 0)
    			return ret;
    		auto iter = dict.lower_bound(num);
    		int k = iter->first;
    		if (k < 100)
    			return " " + iter->second + handle(num % k);
    		else
    			return handle(num / k) + " " + iter->second + handle(num % k);
    	}
    	string numberToWords(int num) {
    		if (num == 0)
    			return "Zero";
    		return handle(num).substr(1);
    	}
    
    	map<int, string, greater<int>> dict;
    };
    
    

Log in to reply
 

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