Leetcode/cpp/171.cpp
2024-06-21 01:36:27 +01:00

19 lines
520 B
C++

#include <string>
class Solution {
private:
constexpr int numericValue(char c) {
return (int) (c-'A');
}
public:
int titleToNumber(std::string columnTitle) {
int value = 0;
int length = columnTitle.length();
for (long power = 1, i = length-1; i >= 0; power *= 26, i--) {
// value += power;
// value += power * numericValue(columnTitle.at(i));
value += power * (1 + numericValue(columnTitle.at(i)));
}
return value;
}
};