-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_273.java
More file actions
60 lines (55 loc) · 1.88 KB
/
Copy pathP_273.java
File metadata and controls
60 lines (55 loc) · 1.88 KB
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
package leetcode.hard;
import java.util.ArrayDeque;
import java.util.Deque;
public class P_273 {
private static final String[] SINGLE = {
"", "One ", "Two ", "Three ", "Four ", "Five ",
"Six ", "Seven ", "Eight ", "Nine "
};
private static final String[] LESS_20 = {
"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ",
"Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ",
"Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ",
"Seventeen ", "Eighteen ", "Nineteen "
};
private static final String[] OVER_20 = {
"", "", "Twenty ", "Thirty ", "Forty ", "Fifty ",
"Sixty ", "Seventy ", "Eighty ", "Ninety "
};
private static final String[] THOUSAND = { "", "Thousand ", "Million ", "Billion " };
public String numberToWords(int num) {
if (num == 0) { return "Zero"; }
final StringBuilder sb = new StringBuilder();
final Deque<Integer> process = new ArrayDeque<>();
int i = 0;
while (num > 0) {
process.addFirst(num % 1000);
i++;
num /= 1000;
}
while (!process.isEmpty()) {
sb.append(convert(process.removeFirst(), --i));
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
private static String convert(int num, int idx) {
final StringBuilder sb = new StringBuilder();
if (num == 0) {
return sb.toString();
}
if (num >= 100) {
sb.append(SINGLE[num / 100]);
sb.append("Hundred ");
num %= 100;
}
if (num <= 19) {
sb.append(LESS_20[num]);
} else {
sb.append(OVER_20[num / 10]);
sb.append(num % 10 == 0 ? "" : SINGLE[num % 10]);
}
sb.append(THOUSAND[idx]);
return sb.toString();
}
}