Skip to content

Commit

Permalink
Fraction to decimal (#197)
Browse files Browse the repository at this point in the history
* add fraction to decimal

* impl fraction to decimal
  • Loading branch information
SKTT1Ryze authored Mar 9, 2024
1 parent b7c3497 commit 8b4e648
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
68 changes: 68 additions & 0 deletions leetcode-cc/FractionToDecimal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <unordered_map>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PFractionToDecimal, 166, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Fraction to Recurring Decimal",
"Given two integers representing the numerator and denominator of a "
"fraction, return the fraction in string format.",
{""});

class SFractionToDecimal : public ISolution {
public:
size_t problemId() const override { return 166; }
string name() const override {
return ("Solution for " + string("Fraction To Recurring Decimal"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<pair<int, int>, string>(
{{1, 2}, {2, 1}, {4, 333}}, {"0.5", "2", "0.(012)"},
[this](auto input) {
return this->fractionToDecimal(input.first, input.second);
});
};
int benchmark() const override { return 0; }

private:
string fractionToDecimal(int numerator, int denominator) const {
if (numerator == 0) return "0";

string result;

if ((numerator < 0) ^ (denominator < 0)) result += '-';

long long num = abs((long long)numerator);
long long den = abs((long long)denominator);

result += to_string(num / den);

long long remainder = num % den;
if (remainder == 0) return result;

result += '.';

unordered_map<long long, int> map;
while (remainder != 0) {
// begin recurring
if (map.find(remainder) != map.end()) {
result.insert(map[remainder], "(");
result += ')';
break;
}

map[remainder] = result.size();

remainder *= 10;
result += to_string(remainder / den);
remainder %= den;
}

return result;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "../leetcode-cc/FindPlayersWithZeroOrOneLosses.hpp"
#include "../leetcode-cc/FlattenBTreeToLinkedList.hpp"
#include "../leetcode-cc/FourSum.hpp"
#include "../leetcode-cc/FractionToDecimal.hpp"
#include "../leetcode-cc/GenerateParentheses.hpp"
#include "../leetcode-cc/GrayCode.hpp"
#include "../leetcode-cc/HouseRobber.hpp"
Expand Down Expand Up @@ -711,5 +712,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SCmpVersionNum>(); });

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PFractionToDecimal>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SFractionToDecimal>(); });

return 0;
}

0 comments on commit 8b4e648

Please sign in to comment.