leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0273.cpp (1497B)
0 class Solution { 1 static string int_string(int n) { 2 static const char *const below_20[] = {"One", "Two", "Three", "Four", "Five", 3 "Six", "Seven", "Eight", "Nine", "Ten", 4 "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", 5 "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; 6 static const char *const below_100[] = {"Twenty", "Thirty", "Forty", "Fifty", 7 "Sixty", "Seventy", "Eighty", "Ninety"}; 8 9 if (n >= 1000000000) 10 return int_string(n / 1000000000) + " Billion" + int_string(n - 1000000000 * (n / 1000000000)); 11 else if (n >= 1000000) 12 return int_string(n / 1000000) + " Million" + int_string(n - 1000000 * (n / 1000000)); 13 else if (n >= 1000) 14 return int_string(n / 1000) + " Thousand" + int_string(n - 1000 * (n / 1000)); 15 else if (n >= 100) 16 return int_string(n / 100) + " Hundred" + int_string(n - 100 * (n / 100)); 17 else if (n >= 20) 18 return string(" ") + below_100[n / 10 - 2] + int_string(n - 10 * (n / 10)); 19 else if (n >= 1) 20 return string(" ") + below_20[n - 1]; 21 22 return ""; 23 } 24 25 public: 26 string numberToWords(int num) const { 27 if (num == 0) return "Zero"; 28 return int_string(num).substr(1); 29 } 30 };