题目
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
For example,
123 -> “One Hundred Twenty Three”
12345 -> “Twelve Thousand Three Hundred Forty Five”
1234567 -> “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
解答
第一次代码是自己短路了一晚上,用最简单粗暴的方式AC了。解法之拙劣,我都看不下去了。
主要的错误在于:
- 没有优先考虑递归,从Billion开始处理
- 没有结合除数取余的思路,设计好数字单词的结构
class Solution {
public:
#define HUND " Hundred"
#define THOU " Thousand"
#define MILL " Million"
#define BILL " Billion"
map<int, string> num2word;
string cut(int number) {
int quotitent, reminder;
string temp;
if (number == 0)
return " ";
if (number > 0 && number <= 20) {
temp += num2word[number];
}
else if (number > 20 && number < 100) {
quotitent = number / 10;
reminder = number % 10;
if (reminder == 0) {
temp += num2word[quotitent*10];
}
else {
temp += num2word[quotitent*10];
temp += " ";
temp += num2word[reminder];
}
}
else if (number >= 100 && number <1000) {
quotitent = number / 100;
reminder = number % 100;
temp += num2word[quotitent];
temp += HUND;
if (reminder == 0)
return temp;
temp += " ";
if (reminder > 0 && reminder <= 20) {
temp += num2word[reminder];
}
else {
quotitent = reminder / 10;
reminder = reminder % 10;
if (reminder == 0) {
temp += num2word[quotitent*10];
}
else {
temp += num2word[quotitent*10];
temp += " ";
temp += num2word[reminder];
}
}
}
return temp;
}
string numberToWords(int num) {
num2word[0] = "Zero";
num2word[1] = "One";
num2word[2] = "Two";
num2word[3] = "Three";
num2word[4] = "Four";
num2word[5] = "Five";
num2word[6] = "Six";
num2word[7] = "Seven";
num2word[8] = "Eight";
num2word[9] = "Nine";
num2word[10] = "Ten";
num2word[11] = "Eleven";
num2word[12] = "Twelve";
num2word[13] = "Thirteen";
num2word[14] = "Fourteen";
num2word[15] = "Fifteen";
num2word[16] = "Sixteen";
num2word[17] = "Seventeen";
num2word[18] = "Eighteen";
num2word[19] = "Nineteen";
num2word[20] = "Twenty";
num2word[30] = "Thirty";
num2word[40] = "Forty";
num2word[50] = "Fifty";
num2word[60] = "Sixty";
num2word[70] = "Seventy";
num2word[80] = "Eighty";
num2word[90] = "Ninety";
if(num == 0)
return num2word[num];
int q, r, i = 0;
string w = "";
string temp[4];
q = num / 1000;
r = num % 1000;
while (q > 0) {
temp[i++] = cut(r);
int t = q;
q = q / 1000;
r = t % 1000;
}
temp[i] = cut(r);
if (i == 3) {
w += temp[3] + BILL;
if(temp[2] != " ")
w += " " + temp[2] + MILL;
if(temp[1] != " ")
w += " " + temp[1] + THOU;
if(temp[0] != " ")
w += " " + temp[0];
}
else if (i == 2) {
w += temp[2] + MILL;
if(temp[1] != " ")
w += " " + temp[1] + THOU;
if(temp[0] != " ")
w += " " + temp[0];
}
else if (i == 1) {
w += temp[1] + THOU;
if(temp[0] != " ")
w += " " + temp[0];
}
else {
w += temp[0];
}
return w;
}
};
第二种解法是参考了讨论区,修正了上面没有考虑好的亮点
- 用递归函数,按照1000的阶数依次进行相除取余
- 使用数组保存0到20和20到90两套单词表,存取顺序对应数字的位置
class Solution {
public:
// define numericiacl words arrays
string digitals[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
string tens[10] = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string int2word(int num) {
if (num >= 1000000000)
return int2word(num / 1000000000) + " Billion" + int2word(num % 1000000000);
else if (num >= 1000000)
return int2word(num / 1000000) + " Million" + int2word(num % 1000000);
else if (num >= 1000)
return int2word(num / 1000) + " Thousand" + int2word(num % 1000);
else if (num >= 100)
return int2word(num / 100) + " Hundred" + int2word(num % 100);
else if (num >= 20)
return " " + tens[num / 10] + int2word(num % 10);
else if (num > 0)
return " " + digitals[num];
else
return "";
}
string numberToWords(int num) {
if (num == 0)
return "Zero";
else {
return int2word(num).substr(1);
}
}
};