Java Solution(easy to understand)


  • 1
    X
    public class Solution {
    public String numberToWords(int num) {
        if(num==0) return "Zero";
        int index=0;
        StringBuffer result=new StringBuffer();
        while(num>0){
          int digits = num % 1000;
          num=num/1000;
          String s=helper(digits);
          if(s!= null && !s.isEmpty()){
              if(result.length()>0) result.insert(0," ");
              switch (index){
                  case 0:
                      break;
                  case 1:
                      s+=" Thousand";
                      break;
                  case 2:
                      s+=" Million";
                      break;
                  case 3:
                      s+=" Billion";
                      break;
              } 
            result.insert(0,s);
          }
          index++;
        }
        return result.toString();
    }
    
    public String helper(int digits){
        int units=0,tens=0,hundreds=0;
        int index=0;
        while(digits>0){
           switch (index){
               case 0:
                   units=digits%10;
                   break;
               case 1:
                   tens=digits%10;
                   break;
               case 2:
                   hundreds=digits%10;
                   break;
           }
           digits=digits/10;
           index++;
        }
        StringBuffer result=new StringBuffer();
        if(hundreds>0){
            switch (hundreds){
                case 1:
                    result.append("One Hundred");
                    break;
                case 2:
                    result.append("Two Hundred");
                    break;
                case 3:
                    result.append("Three Hundred");
                    break;
                case 4:
                    result.append("Four Hundred");
                    break;
                case 5:
                    result.append("Five Hundred");
                    break;
                case 6:
                    result.append("Six Hundred");
                    break;
                case 7:
                    result.append("Seven Hundred");
                    break;
                case 8:
                    result.append("Eight Hundred");
                    break;
                case 9:
                    result.append("Nine Hundred");
                    break;
            }
        }
        
        if(tens>0){
            if(hundreds>0) result.append(" ");
            switch (tens){
                case 1:
                    switch (units){
                        case 0:
                            result.append("Ten");
                            break;
                        case 1:
                            result.append("Eleven");
                            break;
                        case 2:
                            result.append("Twelve");
                            break;
                        case 3:
                            result.append("Thirteen");
                            break;
                        case 4:
                            result.append("Fourteen");
                            break;
                        case 5:
                            result.append("Fifteen");
                            break;
                        case 6:
                            result.append("Sixteen");
                            break;
                        case 7:
                            result.append("Seventeen");
                            break;
                        case 8:
                            result.append("Eighteen");
                            break;
                        case 9:
                            result.append("Nineteen");
                            break; 
                    }
                    break;
                case 2:
                    result.append("Twenty");
                    break;
                case 3:
                    result.append("Thirty");
                    break;
                case 4:
                    result.append("Forty");
                    break;
                case 5:
                    result.append("Fifty");
                    break;
                case 6:
                    result.append("Sixty");
                    break;
                case 7:
                    result.append("Seventy");
                    break;
                case 8:
                    result.append("Eighty");
                    break;
                case 9:
                    result.append("Ninety");
                    break;
            }
        }
        
        if(units>0 && tens!=1){
            if(tens>0 || hundreds>0) result.append(" ");
            switch (units){
                case 1:
                    result.append("One");
                    break;
                case 2:
                    result.append("Two");
                    break;
                case 3:
                    result.append("Three");
                    break;
                case 4:
                    result.append("Four");
                    break;
                case 5:
                    result.append("Five");
                    break;
                case 6:
                    result.append("Six");
                    break;
                case 7:
                    result.append("Seven");
                    break;
                case 8:
                    result.append("Eight");
                    break;
                case 9:
                    result.append("Nine");
                    break;
            }
        }
        return result.toString();
    }
    

    }


  • 0
    L

    public class Solution {
    public String numberToWords(int num) {
    if (num < 0 || num > Integer.MAX_VALUE) {
    return null;
    }
    if (num == 0) {
    return "Zero";
    }
    StringBuffer res = new StringBuffer();
    String[] sb = {"" , "Thousand ", "Million ", "Billion "};
    int j = 0;
    while (num > 0) {
    StringBuffer s = helper(num % 1000);
    if (s != null) {
    res=s.append(sb[j]+res);
    }
    j++;
    num /= 1000;
    }
    return res.toString().trim();
    }

    public static StringBuffer helper(int num) {
    	if (num > 999 | num <= 0)
    		return null;
    	String[] geWei = { "One ", "Two ", "Three ", "Four ", "Five ", "Six ","Seven ", "Eight ", "Nine ", "Ten ", 
    			"Eleven ", "Twelve ","Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ",	"Eighteen ", "Nineteen " };
    	String[] shiWei = { " ", "Twenty ", "Thirty ", "Forty ", "Fifty ","Sixty ", "Seventy ", "Eighty ", "Ninety " };
    	StringBuffer sb = new StringBuffer();
    	if (num > 99) {
    		sb.append(geWei[num / 100 - 1] + "Hundred ");
    		num %= 100;
    	}
    	if (num > 19) {
    		sb.append(shiWei[num / 10 - 1]);
    		num %= 10;
    	}
    	if (num > 0) {
    		sb.append(geWei[num - 1]);
    	}
    	return sb;
    }
    

    }


Log in to reply
 

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