-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path273.integer_to_english_words.cpp
167 lines (139 loc) · 4.03 KB
/
273.integer_to_english_words.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
*
* Example 1:
*
* Input: 123
* Output: "One Hundred Twenty Three"
* Example 2:
*
* Input: 12345
* Output: "Twelve Thousand Three Hundred Forty Five"
* Example 3:
*
* Input: 1234567
* Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
* Example 4:
*
* Input: 1234567891
* Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
*/
/**
* 每三位一算,对于小于20的数还得单独处理,额外还需要处理好空格,没啥好办法,多写些test case慢慢改
*/
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
private:
vector<string> _bits = {
"",
"Thousand",
"Million",
"Billion",
};
unordered_map<int, string> _table = {
{0, "Zero"},
{1, "One"},
{2, "Two"},
{3, "Three"},
{4, "Four"},
{5, "Five"},
{6, "Six"},
{7, "Seven"},
{8, "Eight"},
{9, "Nine"},
{10, "Ten"},
{11, "Eleven"},
{12, "Twelve"},
{13, "Thirteen"},
{14, "Fourteen"},
{15, "Fifteen"},
{16, "Sixteen"},
{17, "Seventeen"},
{18, "Eighteen"},
{19, "Nineteen"},
{20, "Twenty"},
{30, "Thirty"},
{40, "Forty"},
{50, "Fifty"},
{60, "Sixty"},
{70, "Seventy"},
{80, "Eighty"},
{90, "Ninety"},
};
public:
string numberToWords(int num) {
if (num == 0) {
return _table[num];
}
string result;
int n = num;
int l = num;
int i = -1;
while (l > 0) {
string temp;
n = l % 1000;
l = l / 1000;
i++;
if (n >= 100) {
temp.append(hundredBuild(n));
} else {
temp.append(littleBuild(n));
}
if (n != 0) {
temp.push_back(' ');
temp.append(_bits[i]);
temp.push_back(' ');
}
result.insert(result.begin(), temp.begin(), temp.end());
}
return result.erase(result.find_last_not_of(' ') + 1);
}
inline string hundredBuild(int num) {
string temp;
int hundred = num / 100;
int hundred_mod = num % 100;
temp.append(_table[hundred]);
temp.push_back(' ');
temp.append("Hundred");
if (hundred_mod != 0) {
temp.push_back(' ');
temp.append(littleBuild(hundred_mod));
}
return temp;
}
inline string littleBuild(int num) {
string temp;
if (num > 20) {
int tens = num / 10 * 10;
int bits = num % 10;
temp.append(_table[tens]);
if (bits != 0) {
temp.push_back(' ');
temp.append(_table[bits]);
}
} else if (num > 0) {
temp.append(_table[num]);
}
return temp;
}
};
int main() {
Solution s;
assert(s.numberToWords(0) == "Zero");
assert(s.numberToWords(1) == "One");
assert(s.numberToWords(12) == "Twelve");
assert(s.numberToWords(30) == "Thirty");
assert(s.numberToWords(123) == "One Hundred Twenty Three");
assert(s.numberToWords(1000) == "One Thousand");
assert(s.numberToWords(12345) == "Twelve Thousand Three Hundred Forty Five");
assert(s.numberToWords(50868) == "Fifty Thousand Eight Hundred Sixty Eight");
assert(s.numberToWords(1000000) == "One Million");
assert(s.numberToWords(1234567) == "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven");
assert(s.numberToWords(1000000000) == "One Billion");
assert(s.numberToWords(1234567891) ==
"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One");
return 0;
}