Skip to content

Commit

Permalink
Excel sheet column title (#199)
Browse files Browse the repository at this point in the history
* add excel sheet column title

* impl excel sheet column title
  • Loading branch information
SKTT1Ryze authored Mar 11, 2024
1 parent b7564b2 commit e9b7c16
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
41 changes: 41 additions & 0 deletions leetcode-cc/ExcelSheetColTitle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PExcelSheetColTitle, 168, DIFFI_EASY, TOPIC_ALGORITHMS,
"Excel Sheet Column Title",
"Given an integer columnNumber, return its corresponding column title as "
"it appears in an Excel sheet.",
{"Math"});

class SExcelSheetColTitle : public ISolution {
public:
size_t problemId() const override { return 168; }
string name() const override {
return ("Solution for " + string("Excel Sheet Column Title"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<int, string>(
{1, 28, 701}, {"A", "AB", "ZY"},
[this](auto input) { return this->convertToTitle(input); });
};
int benchmark() const override { return 0; }

private:
string convertToTitle(int columnNumber) const {
string res = "";

while (columnNumber) {
columnNumber--;
int mod = columnNumber % 26;
res = char('A' + mod) + res;
columnNumber /= 26;
}

return res;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "../leetcode-cc/DistinctSubsequences.hpp"
#include "../leetcode-cc/DivideTwoIntegers.hpp"
#include "../leetcode-cc/EvaluateReversePolishNotation.hpp"
#include "../leetcode-cc/ExcelSheetColTitle.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArray.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArrayII.hpp"
#include "../leetcode-cc/FindPeakElement.hpp"
Expand Down Expand Up @@ -725,5 +726,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
return std::make_shared<STwoSumIIInputArraySorted>();
});

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

return 0;
}

0 comments on commit e9b7c16

Please sign in to comment.