Skip to content

Commit

Permalink
Factorial trailing zeroes (#202)
Browse files Browse the repository at this point in the history
* add factorial trailing zeroes

* impl factorial trailing zeroes
  • Loading branch information
SKTT1Ryze authored Mar 12, 2024
1 parent e467572 commit 542a579
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
38 changes: 38 additions & 0 deletions leetcode-cc/FactorialTrailingZeroes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stack>

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

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PFactorialTrailingZeroes, 172, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Factorial Trailing Zeroes",
"Given an integer n, return the number of trailing zeroes in n!.",
{"Math"});

class SFactorialTrailingZeroes : public ISolution {
public:
size_t problemId() const override { return 172; }
string name() const override {
return ("Solution for " + string("Factorial Trailing Zeroes"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<int, int>(
{3, 5, 0, 6, 18}, {0, 1, 0, 1, 3},
[this](auto input) { return this->trailingZeroes(input); });
};
int benchmark() const override { return 0; }

private:
int trailingZeroes(int n) const {
int count = 0;
while (n > 0) {
count += n / 5;
n /= 5;
}
return count;
}
};
2 changes: 1 addition & 1 deletion leetcode-cc/FractionToDecimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ IMPLEMENT_PROBLEM_CLASS(
"Fraction to Recurring Decimal",
"Given two integers representing the numerator and denominator of a "
"fraction, return the fraction in string format.",
{""});
{"Math"});

class SFractionToDecimal : public ISolution {
public:
Expand Down
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../leetcode-cc/EvaluateReversePolishNotation.hpp"
#include "../leetcode-cc/ExcelSheetColNum.hpp"
#include "../leetcode-cc/ExcelSheetColTitle.hpp"
#include "../leetcode-cc/FactorialTrailingZeroes.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArray.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArrayII.hpp"
#include "../leetcode-cc/FindPeakElement.hpp"
Expand Down Expand Up @@ -743,5 +744,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SExcelSheetColNum>(); });

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

return 0;
}

0 comments on commit 542a579

Please sign in to comment.