-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_273.java
69 lines (63 loc) · 2.19 KB
/
_273.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.fishercoder.solutions.firstthousand;
public class _273 {
public static class Solution1 {
private String[] belowTen =
new String[] {
"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
};
private String[] belowTwenty =
new String[] {
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
};
private String[] belowHundred =
new String[] {
"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
private String[] overThousand = new String[] {"Thousand", "Million", "Billion"};
public String numberToWords(int num) {
String result;
if (num == 0) {
return belowTen[num];
}
result = hundredHelper(num % 1000);
num = num / 1000;
int i = 0;
while (i < 3 && num > 0) {
if (num % 1000 > 0) {
result = hundredHelper(num % 1000) + overThousand[i] + " " + result;
}
num = num / 1000;
i++;
}
return result.trim();
}
private String hundredHelper(int num) {
String nstr = "";
if (num >= 100) {
nstr = belowTen[num / 100] + " Hundred ";
}
num = num % 100;
if (num >= 20) {
if (num % 10 != 0) {
nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " ";
} else {
nstr = nstr + belowHundred[num / 10 - 2] + " ";
}
} else if (num >= 10) {
nstr = nstr + belowTwenty[num % 10] + " ";
} else if (num > 0) {
nstr = nstr + belowTen[num] + " ";
}
return nstr;
}
}
}