This problem looks busy at first, but the accepted solution is built around one steady invariant. For 273. Integer to English Words, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are join, numberToWords.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 91.89% of C++ online submissions for Integer to English Words.
02//Memory Usage: 8 MB, less than 23.25% of C++ online submissions for Integer to English Words.
03class Solution {
04public:
05 vector<string> tokens;
06 vector<string> ones;
07 vector<string> tens;
08 vector<string> teens;
09
10 template <typename Iter>
11 std::string join(Iter begin, Iter end, std::string const& separator)
12 {
13 std::ostringstream result;
14 result.precision(2); //for floating point
15 if (begin != end)
16 result << *begin++;
17 while (begin != end)
18 //std::fixed : for floating point
19 result << std::fixed << separator << *begin++;
20 return result.str();
21 }
22
23 void numberToWords(int num, int exp) {
24 //to solve 1000000
25 if(num == 0 && exp > 0) return;
26
27 if(num >= 100){
28 tokens.push_back(ones[num/100]);
29 tokens.push_back("Hundred");
30 num %= 100;
31 }
32
33 if(num >= 11 && num <= 19){
34 tokens.push_back(teens[num-11]);
35 num = 0;
36 }
37
38 if(num >= 10){
39 tokens.push_back(tens[num/10-1]);
40 num %= 10;
41 }
42
43 if(num > 0){
44 tokens.push_back(ones[num]);
45 }else if(tokens.empty() && num == 0 && exp == 0){
46 //add "Zero" only when there is no previous digits
47 tokens.push_back(ones[0]);
48 }
49
50 if(!tokens.empty()){
51 if(exp == 3){
52 tokens.push_back("Thousand");
53 }else if(exp == 6){
54 tokens.push_back("Million");
55 }else if(exp == 9){
56 tokens.push_back("Billion");
57 }
58 }
59 };
60
61 string numberToWords(int num) {
62 ones = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
63 tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
64 teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
65
66 for(int exp = 9; exp >= 0; exp -= 3){
67 // cout << "num: " << num << endl;
68 numberToWords(num/pow(10,exp), exp);
69 num %= (int)pow(10, exp);
70 }
71
72 return join(tokens.begin(), tokens.end(), " ");
73 }
74};
Cost