leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit ca75407896b7dde4a0edd1d11c575cd6b699b96e
parent fc65f7171f2500b65ed613e29ccdb7c511a15275
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  7 Aug 2024 19:33:26 +0200

Daily Problem

Diffstat:
AProblems/0273.cpp | 31+++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/Problems/0273.cpp b/Problems/0273.cpp @@ -0,0 +1,31 @@ +class Solution { + static string int_string(int n) { + static const char *const below_20[] = {"One", "Two", "Three", "Four", "Five", + "Six", "Seven", "Eight", "Nine", "Ten", + "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", + "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; + static const char *const below_100[] = {"Twenty", "Thirty", "Forty", "Fifty", + "Sixty", "Seventy", "Eighty", "Ninety"}; + + if (n >= 1000000000) + return int_string(n / 1000000000) + " Billion" + int_string(n - 1000000000 * (n / 1000000000)); + else if (n >= 1000000) + return int_string(n / 1000000) + " Million" + int_string(n - 1000000 * (n / 1000000)); + else if (n >= 1000) + return int_string(n / 1000) + " Thousand" + int_string(n - 1000 * (n / 1000)); + else if (n >= 100) + return int_string(n / 100) + " Hundred" + int_string(n - 100 * (n / 100)); + else if (n >= 20) + return string(" ") + below_100[n / 10 - 2] + int_string(n - 10 * (n / 10)); + else if (n >= 1) + return string(" ") + below_20[n - 1]; + + return ""; + } + + public: + string numberToWords(int num) const { + if (num == 0) return "Zero"; + return int_string(num).substr(1); + } +}; diff --git a/README.md b/README.md @@ -252,6 +252,7 @@ for solving problems. | 0263 | Easy | [Ugly Number](Problems/0263.cpp) | | 0264 | Medium | [Ugly Number II](Problems/0264.cpp) | | 0268 | Easy | [Missing Number](Problems/0268.cpp) | +| 0273 | Hard | [Integer to English Words](Problems/0273.cpp) | | 0274 | Medium | [H-Index](Problems/0274.cpp) | | 0278 | Easy | [First Bad Version](Problems/0278.cpp) | | 0279 | Medium | [Perfect Squares](Problems/0279.cpp) |