The Excel Sheet Column Number solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Excel Sheet Column Number
C++, Python and TypeScript solutions for Excel Sheet Column Number.
Solution files
Python•excel-sheet-column-number/synced-solution.py
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for char in columnTitle:
result = result * 26 + (ord(char) - ord('A') + 1)
return result
C++•excel-sheet-column-number/synced-solution.cpp
class Solution {
public:
int titleToNumber(string columnTitle) {
int result = 0;
for (char c : columnTitle) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
};
TypeScript•excel-sheet-column-number/synced-solution.ts
function titleToNumber(columnTitle: string): number {
let result = 0;
for (const char of columnTitle) {
result = result * 26 + (char.charCodeAt(0) - 65 + 1);
}
return result;
}