Very easy straightforward Java solution.

  • 0
    public class Solution {
        public String numberToWords(int num) {
            String[] di = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
            String[] ten = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"};
            String[] tens = new String[]{"","","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
            String[] e = new String[]{"Billion","Million","Thousand"};
            int exp = 1000000000;
            StringBuilder res = new StringBuilder();
            if(num==0)return di[0];
            for(int i=0;i<4;i++){
                int tmp = num/exp;
                num %= exp;
                    int h=tmp/100;
                    int t=(tmp%100)/10;
                    int d=tmp%10;
                    if(h!=0)res.append(di[h]+" Hundred ");
                    if(t==1)res.append(ten[tmp%100-10]+" ");
                        if(t!=0)res.append(tens[t]+" ");
                        if(d!=0)res.append(di[d]+" ");
                    if(i<3)res.append(e[i]+" ");
                exp /= 1000;
            return res.toString().trim();

Log in to reply

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