Skip to content

Commit

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

* impl excel sheet column number

* fix linux error
  • Loading branch information
SKTT1Ryze authored Mar 11, 2024
1 parent ea1a6e1 commit e467572
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions leetcode-cc/ExcelSheetColNum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <cmath>

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

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PExcelSheetColNum, 171, DIFFI_EASY, TOPIC_ALGORITHMS,
"Excel Sheet Column Number",
"Given a string columnTitle that represents the column title as appears in "
"an Excel sheet, return its corresponding column number.",
{"Math"});

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

private:
int titleToNumber(string columnTitle) const {
int n = columnTitle.size();
int k = 0;
int res = 0;

for (int i = n - 1; i >= 0; i--) {
int num = columnTitle[i] - 'A' + 1;
res += num * pow(26, k);
k++;
}

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/ExcelSheetColNum.hpp"
#include "../leetcode-cc/ExcelSheetColTitle.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArray.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArrayII.hpp"
Expand Down Expand Up @@ -737,5 +738,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SMajorityElement>(); });

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

return 0;
}

0 comments on commit e467572

Please sign in to comment.