-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path273_Integer_to_English_Words.cpp
More file actions
59 lines (53 loc) · 1.85 KB
/
273_Integer_to_English_Words.cpp
File metadata and controls
59 lines (53 loc) · 1.85 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
/*
273. Integer to English Words
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
0 <= num <= 231 - 1
*/
class Solution {
public:
string numberToWords(int num) {
if (num == 0) return "Zero";
string bigString[] = {"Thousand", "Million", "Billion"};
string result = numberToWordsHelper(num % 1000);
num /= 1000;
for (int i = 0; i < 3; ++i) {
if (num > 0 && num % 1000 > 0) {
result = numberToWordsHelper(num % 1000) + bigString[i] + " " + result;
}
num /= 1000;
}
return result.empty() ? result : result.substr(0, result.size() - 1); // Trim trailing space
}
string numberToWordsHelper(int num) {
string digitString[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
string teenString[] = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
string tenString[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string result = "";
if (num > 99) {
result += digitString[num / 100] + " Hundred ";
}
num %= 100;
if (num < 20 && num > 9) {
result += teenString[num - 10] + " ";
} else {
if (num >= 20) {
result += tenString[num / 10] + " ";
}
num %= 10;
if (num > 0) {
result += digitString[num] + " ";
}
}
return result;
}
};