-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerToEnglishWords.java
33 lines (29 loc) · 1.51 KB
/
IntegerToEnglishWords.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
/* https://leetcode.com/problems/integer-to-english-words/submissions/1347841744/?envType=daily-question&envId=2024-08-07 */
/* 273. Integer to English Words */
class IntegerToEnglishWords {
public String numberToWords(int num) {
return num == 0 ? "Zero" : helper(num);
}
private final String[] belowTwenty = {"", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] tens = {"", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private String helper(int num) {
StringBuilder s = new StringBuilder();
if (num < 20)
s.append(belowTwenty[num]);
else if (num < 100)
s.append(tens[num / 10]).append(" ").append(belowTwenty[num % 10]);
else if (num < 1000)
s.append(helper(num / 100)).append(" Hundred ").append(helper(num % 100));
else if (num < 1000000)
s.append(helper(num / 1000)).append(" Thousand ").append(helper(num % 1000));
else if (num < 1000000000)
s.append(helper(num / 1000000)).append(" Million ").append(helper(num % 1000000));
else
s.append(helper(num / 1000000000)).append(" Billion ").append(helper(num % 1000000000));
return s.toString().trim();
}
}