Java - Appending with StringBuilder


  • 0
    D
    public class Solution {
    
        public static String numberToWords(int num) {
            if(num==0) 
                return zeroText;
    
            int pseudoLength = calcNumberOfPseudoDigits(num);
            int outerLength= pseudoLength/3;
        	StringBuilder sb = new StringBuilder("");
        	
        	int[][] digArray = new int[outerLength][3];
        	for(int i=0; i<outerLength; i++){
        		for(int j=0; j<3; j++){
        			digArray[i][j]=num%10;
        			num/=10;
        		}
        	}
            
            for (int i=outerLength-1; i >= 0; i--) {
                int huns = digArray[i][2],
                	tens = digArray[i][1],
                	ones = digArray[i][0];
    
                if(huns > 0){
                    sb.append(onesText[huns]).append(hundredText);
                    if(i>0 && tens==0 && ones==0)
                        sb.append(thousandsText[i]);
                }
                if(tens > 0){
                	if (tens!=1) {
                		sb.append(tensText[tens]);
                	} else {
    					sb.append(betweensText[ones]);
                		ones=0;
                	}
                    if(i>0 && ones==0)
                        sb.append(thousandsText[i]);
                }
                if(ones > 0){
                    sb.append(onesText[ones]);
                    if(i>0)
                    	sb.append(thousandsText[i]);
                }
        	}
            return sb.toString().trim();
        }
        
        private static int calcNumberOfPseudoDigits(int number){
        	int digits;
        	if (number<100) {
        		digits=3;
        	}else if (number<100000) {
        		digits=6;
        	}else if (number<100000000) {
        		digits=9;
        	}else{
        		digits=12;
        	}
        	return digits;
        }
    
        private static final String zeroText = "Zero";
    
        private static final String[] onesText = {
            "",
            " One",
            " Two",
            " Three",
            " Four",
            " Five",
            " Six",
            " Seven",
            " Eight",
            " Nine",
        };
    
        private static final String[] betweensText = {
            " Ten",
            " Eleven",
            " Twelve",
            " Thirteen",
            " Fourteen",
            " Fifteen",
            " Sixteen",
            " Seventeen",
            " Eighteen",
            " Nineteen"
        };  
    
        private static final String[] tensText = {
            "",
            " Ten",
            " Twenty",
            " Thirty",
            " Forty",
            " Fifty",
            " Sixty",
            " Seventy",
            " Eighty",
            " Ninety"
        };
    
        private static final String hundredText = " Hundred";
        
    	private static final String[] thousandsText = {
    		"",
    		" Thousand", // 1 comma   ex: 1,000
    		" Million",  // 2 commas  ex: 1,000,000
    		" Billion"   // 3 commas  ex: 1,000,000,000
    	};
    
        public static void main(String[] args) {
            System.out.println(numberToWords(Integer.valueOf(args[0])));
        }
    
    }

Log in to reply
 

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