../ Competitive Programming
-rw-r--r--aguang2.5 KiB2 min read

Leet Code Daily 2024-08-07 - Integer to English Words

Solution

Simulation.

Code

c++
constexpr int kBillion = 1E9;
constexpr int kMillion = 1E6;
constexpr int kThousand = 1E3;
constexpr int kHundred = 1E2;
constexpr int kTwenty = 2E1;

const vector<string> kWordForNumberUpTo19 = {
    "Zero",    "One",     "Two",       "Three",    "Four",
    "Five",    "Six",     "Seven",     "Eight",    "Nine",
    "Ten",     "Eleven",  "Twelve",    "Thirteen", "Fourteen",
    "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
};

const vector<string> kWordForNumberDivisibleBy10 = {
    "Zero",  "Ten",   "Twenty",  "Thirty", "Forty",
    "Fifty", "Sixty", "Seventy", "Eighty", "Ninety",
};

const string kWordForHundred = "Hundred";
const string kWordForThousand = "Thousand";
const string kWordForMillion = "Million";
const string kWordForBillion = "Billion";

class Solution {
public:
    void numberToWordsUpTo999(int num, vector<string>& out) {
        if (num == 0) {
            out.push_back(kWordForNumberUpTo19[0]);
            return;
        }
        if (num >= kHundred) {
            out.push_back(kWordForNumberUpTo19[num / kHundred]);
            out.push_back(kWordForHundred);
            num %= kHundred;
        }
        if (num > 0 && num <= 19) {
            out.push_back(kWordForNumberUpTo19[num]);
        } else if (num >= 20) {
            out.push_back(kWordForNumberDivisibleBy10[num / 10]);
            if (num % 10 > 0) {
                out.push_back(kWordForNumberUpTo19[num % 10]);
            }
        }
    }

    string numberToWords(int num) {
        if (num == 0) {
            return kWordForNumberUpTo19[0];
        }
        vector<string> out;
        if (num >= kBillion) {
            numberToWordsUpTo999(num / kBillion, out);
            out.push_back(kWordForBillion);
            num %= kBillion;
        }
        if (num >= kMillion) {
            numberToWordsUpTo999(num / kMillion, out);
            out.push_back(kWordForMillion);
            num %= kMillion;
        }
        if (num >= kThousand) {
            numberToWordsUpTo999(num / kThousand, out);
            out.push_back(kWordForThousand);
            num %= kThousand;
        }
        if (num > 0) {
            numberToWordsUpTo999(num, out);
        }
        string ret = "";
        for (int i = 0; i < out.size(); i++) {
            if (i > 0) {
                ret.append(" ");
            }
            ret.append(out[i]);
        }
        return ret;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window