Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorial trailing zeroes #202

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading