BiruLyu
6/21/2017 - 3:28 AM

273. Integer to English Words(1st).java

class Solution(object):
    
    def numberToWords(self, num):
        """
        :type num: int
        :rtype: str
        """
        
        numericDict = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
        
        decimalDict = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
        
        thousandsGroup = ['Thousand', 'Million', 'Billion']
        
        def integerToWord(num):
            if num == 0:
                return []
            if num < 20:
                return [numericDict[num - 1]]
            if num < 100:
                return [decimalDict[num/10 - 2]] + integerToWord(num%10)
            if num < 1000:
                return [numericDict[num/100 - 1]] + ['Hundred'] + integerToWord(num%100)
            for i in xrange(len(thousandsGroup)):
                if num < 1000**(i+2):
                    return integerToWord(num/1000**(i+1)) + [thousandsGroup[i]] + integerToWord(num%1000**(i+1))
        return ' '.join(integerToWord(num)) or 'Zero'
public class Solution {
    private final String[] to19 = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private final String[] to90 = new String[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private final String[] thousandsGroup = new String[] {"Thousand", "Million","Billion"};
    
    public String helper(int num){
        String res = new String();
        if(num == 0) res = "";
        else if(num < 20) res = to19[num - 1];
        else if(num < 100) res = to90[num/10 - 2] + ' ' + helper(num%10);
        else if(num < 1000) res = to19[num/100 - 1] + " Hundred " + helper(num % 100);
        else if(num < 1000000) res = helper(num/1000) + " Thousand " + helper(num % 1000);
        else if(num < 1000000000) res = helper(num/1000000) + " Million " + helper(num % 1000000);
        else res = helper(num / 1000000000) + " Billion " + helper(num % 1000000000);
        return res.trim();
    }
    public String numberToWords(int num) {
        String res = new String();
        if(num == 0)
            res =  "Zero";
        else
            res =  helper(num).trim();
        return res;
        
    }
}
public class Solution {
    public static final String[] LESSTHAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    public static final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    public static final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
    
    
    
    public String numberToWords(int num) {
        if(num == 0){
            return "Zero";
        }
        
        String words = "";
        int i = 0;
        while(num > 0){
            if(num % 1000 != 0){
                words = helper(num % 1000) + THOUSANDS[i] + " " + words;
            }
            num /= 1000;
            i++;
        }    
        return words.trim();
    }
    
    private String helper(int num){
        if(num == 0){
            return "";
        }else if(num < 20){
            return  LESSTHAN_20[num] + " ";
        }else if(num < 100){
            return  TENS[num / 10] + " " + helper(num % 10);
        }else
            return LESSTHAN_20[num / 100] + " Hundred " + helper(num % 100);
    }
}