# My ugly Java submission

• First, divide the problem to `one`, `two`, `three`, `thousand`, `million`, `billion` parts, according to the # digits of the input.

Then just concatenate the corresponding strings.

While there are soooo many edge cases and I've submitted 12 times to get an AC. (Hardly to find a job absolutely, :(

``````public class Solution {
String ret;
String[] ones, stens, tens;
public String numberToWords(int num) {
ret = new String("");
ones =  new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
stens =  new String[]{"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
tens =  new String[]{"Twenty", "Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
int d = getDigit(num);
if(num==0) return new String("Zero");
if(d==1) one(num);
else if(d==2) two(num);
else if(d==3) three(num);
else if(d>3 && d<7) thousand(num);
else if(d>6 && d<10) million(num);
else if(d>9) billion(num);
ret = ret.replaceAll("\\s+", " ");
return ret.trim();
}
private int getDigit(int num){
int cnt=1;
for(;num/10!=0;cnt++)
num/=10;
return cnt;
}
private void billion(int num){
int bil = num/1000000000;
one(bil);
ret += "Billion ";
if(num%1000000000!=0)
million(num%1000000000);
}
private void million(int num){
int mil = num/1000000;
if(mil>99) three(mil);
else if(mil>10) two(mil);
else if(mil>0) one(mil);
if(mil>0) ret+= "Million ";
if(num%1000000!=0)
thousand(num%1000000);
}
private void thousand(int num){
int thou = num/1000;
if(thou>99) three(thou);
else if(thou>10) two(thou);
else if(thou>0) one(thou);
if(thou>0) ret+= "Thousand ";
if(num%1000!=0)
three(num%1000);
}
//0~10
private void one(int num){
ret += ones[num]+" ";
}
//11-99
private void two(int num){
if(num<10){
one(num);
return;
}
if(num==10) ret+= "Ten ";
if(num>10 && num<20) ret= ret + stens[num-11] + " ";
if(num>19) ret = ret + tens[num/10-2] + " " + ones[num%10] + " ";
}
//100-999
private void three(int num){
if(num<100){
two(num);
return;
}
ret= ret + ones[num/100] + " Hundred ";
two(num%100);
}
}``````

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