# Share my C++ solution, easy to understand.

• ``````class Solution {
public:
Solution() : range({1000000000, 1000000, 1000}),
label({"Billion", "Million", "Thousand"}),
singleNum({"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}),
overTwenty({"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}),
underTwenty({"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}) {}

string numberToWords(int num) {
if (0 == num) {
return "Zero";
}
string result;
for (int i = 0; i < range.size(); ++i) {
int q = num / range[i];
num -= q * range[i];
if (0 != q) {
appendNum(result, q);
result += " " + label[i];
}
}
appendNum(result, num);
return result;
}
private:
inline void appendNum(string& str, int num) {
int q;
if (100 <= num) {
str += str.empty() ? "" : " ";
q = num / 100;
str += singleNum[q - 1];
num -= q * 100;
str += " Hundred";
}
if (10 <= num && num <= 19) {
str += str.empty() ? "" : " ";
str += underTwenty[num - 10];
} else if (20 <= num) {
str += str.empty() ? "" : " ";
q = num / 10;
str += overTwenty[q - 2];
num -= q * 10;
}
if (10 > num && num) {
str += str.empty() ? "" : " ";
str += singleNum[num - 1];
}
}

vector<int> range;
vector<string> label;
vector<string> singleNum;
vector<string> overTwenty;
vector<string> underTwenty;
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.