具体思路:
个人想的是int的范围最大是10^9位,因此只需要考虑billion和million就可以;
将数字填充到12位,建立百位的转化,因此可以变为:
3
位
+
b
i
l
l
i
o
n
+
3
位
+
m
i
l
l
i
o
n
+
6
位
3位+billion+3位+million+6位
3位+billion+3位+million+6位;
其中6位又可以变成3位千+三位;
但是递归更好,这个题要二刷,恶心的一笔;
具体代码:
1.迭代版本:
class Solution {
public:
string numberToWords(int num) {
if(num==0)
return "Zero";
string s="";
while(num!=0){
s.push_back(num%10+'0');
num/=10;
}
reverse(s.begin(), s.end());
vector<string> temp=all_charge(s);
return output(temp);
}
string output(vector<string> vec){
string ret="";
for(int i=0;i<vec.size();i++){
if(i==0){
ret+=vec[i];
}else{
ret+=" "+vec[i];
}
}
return ret;
}
vector<string> all_charge(string s){
while(s.size()!=12){
s='0'+s;
}
vector<string>ret;
string p1=s.substr(0,6);
string pp1=p1.substr(0,3);
string pp2=p1.substr(3,3);
string p2=s.substr(6,6);
vector<string> rp2=charge(p2);
vector<string> rpp1=three_charge(pp1);
vector<string> rpp2=three_charge(pp2);
if(rpp1.size()!=0){
ret.insert(ret.end(), rpp1.begin(),rpp1.end());
ret.push_back("Billion");
}
if(rpp2.size()!=0)
ret.insert(ret.end(), rpp2.begin(),rpp2.end());
if(rpp2.size()!=0){
ret.push_back("Million");
}
ret.insert(ret.end(),rp2.begin(),rp2.end());
return ret;
}
vector<string> charge(string s){
//最多六位,低三位为百,高三位为千;
while(s.size()!=6){
s='0'+s;
}
vector<string>ret;
vector<string> p1=three_charge(s.substr(0,3));
vector<string> p2=three_charge(s.substr(3,3));
if(p1.size()!=0){
ret.insert(ret.end(),p1.begin(),p1.end());
ret.push_back("Thousand");
}
ret.insert(ret.end(),p2.begin(),p2.end());
return ret;
}
vector<string> three_charge(string s){
while(s.size()!=0&&s[0]=='0')
s.erase(s.begin());
vector<string> ret;
if(s.size()==3){
ret.push_back(low_digit[s[0]-'1']);
ret.push_back("Hundred");
s.erase(s.begin());
}
while(s.size()!=0&&s[0]=='0')
s.erase(s.begin());
if(s.size()==2){
//去除前导零仍为两位;
if(s[0]=='1'){
ret.push_back(high_digit[s[1]-'0']);
}else{
ret.push_back(ten_digit[s[0]-'2']);
if(s[1]!='0'){
ret.push_back(low_digit[s[1]-'1']);
}
}
}else if(s.size()==1){
//去除前导0为一位;
ret.push_back(low_digit[s[0]-'1']);
}
return ret;
}
private:
vector<string> low_digit={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
vector<string> high_digit={"Ten","Eleven","Twelve","Thirteen" ,"Fourteen" ,"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
vector<string> ten_digit={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
};
2.递归版本:
class Solution {
public:
vector<string> singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
vector<string> teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string comma(int n) {
string ans = "";
int hundred = n / 100;
if (hundred) {
ans += singles[hundred] + " Hundred ";
}
n = n % 100;
if (n < 20 && n >= 10) {
ans += teens[n%10] + " ";
return ans;
}
if (n >= 20) {
ans += tens[n/10] + " ";
}
n = n % 10;
if (n == 0) return ans;
ans += singles[n%10] + " ";
return ans;
}
string recur(int n) {
int billion = n / 1000000000;
n %= 1000000000;
int million = n / 1000000;
n %= 1000000;
int thousand = n / 1000;
n %= 1000;
string ans = "";
if (billion) {
ans += recur(billion) + "Billion ";
}
if (million) {
ans += recur(million) + "Million ";
}
if (thousand) {
ans += recur(thousand) + "Thousand ";
}
ans += comma(n);
return ans;
}
string numberToWords(int num) {
if (num == 0) return "Zero";
string ans = recur(num);
ans.pop_back();
return ans;
}
};