My clean Java Solution.


  • 0
    C
    public String numberToWords(int num) {
            List<Integer> list = new ArrayList<>();
            if(num==0)
                return "Zero";
            while(num!=0){
                list.add(num%1000);
                num = num/1000;   
            }
            StringBuilder sb = new StringBuilder();
            for(int i=list.size()-1; i>=0; i--){
                sb.append(helper(list.get(i)));
                if(list.get(i)>0){
                    if(i==3)
                        sb.append(" Billion");
                     else if(i==2)
                        sb.append(" Million");
                    else if(i==1)
                         sb.append(" Thousand");
                }
            }
        
            return sb.toString().trim();
        }
        
        public String helper(int num){
            StringBuilder sb  = new StringBuilder();
            int n = num/100;
            if(n>0){
                sb.append(digitToWord(n));
                sb.append(" Hundred");
            }
            num = num%100;
            if(num<=20)
                sb.append(digitToWord(num));
            else{
                n = num/10;
                sb.append(digitToWord(n*10));
                sb.append(digitToWord(num%10));
            }
        return sb.toString();
        }
        
        public String digitToWord(int n){
            StringBuilder sb = new StringBuilder();
            switch(n){
                    case 1:
                        sb.append(" One");
                        break;
                    case 2:
                        sb.append(" Two");
                        break;
                    case 3:
                        sb.append(" Three");
                        break;
                    case 4:
                        sb.append(" Four");
                        break;
                    case 5:
                        sb.append(" Five");
                        break;
                    case 6:
                        sb.append(" Six");
                        break;
                    case 7:
                        sb.append(" Seven");
                        break;
                    case 8:
                        sb.append(" Eight");
                        break;
                    case 9:
                        sb.append(" Nine");
                        break;
                    case 10:
                        sb.append(" Ten");
                        break;
                    case 11:
                        sb.append(" Eleven");
                        break;
                    case 12:
                        sb.append(" Twelve");
                        break;
                    case 13:
                        sb.append(" Thirteen");
                        break;
                    case 14:
                        sb.append(" Fourteen");
                        break;
                    case 15:
                        sb.append(" Fifteen");
                        break;
                    case 16:
                        sb.append(" Sixteen");
                        break;
                    case 17: 
                        sb.append(" Seventeen");
                        break;
                    case 18:
                        sb.append(" Eighteen");
                        break;
                    case 19:
                        sb.append(" Nineteen");
                        break;
                    case 20:
                        sb.append(" Twenty");
                        break;
                    case 30:
                        sb.append(" Thirty");
                        break;
                    case 40:
                        sb.append(" Forty");
                        break;
                    case 50:
                        sb.append(" Fifty");
                        break;
                    case 60:
                        sb.append(" Sixty");
                        break;
                    case 70:
                        sb.append(" Seventy");
                        break;
                    case 80:
                        sb.append(" Eighty");
                        break;
                    case 90:
                        sb.append(" Ninety");
                        break;
                }
                return sb.toString();
        }

Log in to reply
 

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