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

Excel sheet column number #201

Merged
merged 3 commits into from
Mar 11, 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
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;
}
Loading