Integer to English Words
https://leetcode.com/problems/integer-to-english-words/
class Solution {
public:
vector<string> twenty;
vector<string> tens;
vector<string> thousands;
string numberToWords(int num) {
if(num == 0)
{
return "Zero";
}
twenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
thousands = {"", "Thousand", "Million", "Billion"};
int i = 0;
string words;
while(num > 0)
{
if(num%1000 != 0)
{
words = helper(num%1000) + thousands[i] + " " + words;
}
num = num/1000;
i++;
}
while(words.back() == ' ')
{
words.pop_back();
}
return words;
}
string helper(int num)
{
if(num == 0)
{
return "";
}
else if(num < 20)
{
return twenty[num] + " ";
}
else if(num < 100)
{
return tens[num/10] + " " + helper(num%10);
}
else
{
return twenty[num/100] + " Hundred " + helper(num%100);
}
}
};