给你一个字符串 columnTitle
,表示 Excel 表格中的列名称。返回 该列名称对应的列序号 。
例如:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
示例 1:
输入: columnTitle = "A" 输出: 1
示例 2:
输入: columnTitle = "AB" 输出: 28
示例 3:
输入: columnTitle = "ZY" 输出: 701
提示:
1 <= columnTitle.length <= 7
columnTitle
仅由大写英文组成columnTitle
在范围["A", "FXSHRXW"]
内
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
for c in columnTitle:
res = res * 26 + (ord(c) - ord('A') + 1)
return res
class Solution {
public int titleToNumber(String columnTitle) {
int res = 0;
for (char c : columnTitle.toCharArray()) {
res = res * 26 + (c - 'A' + 1);
}
return res;
}
}
function titleToNumber(columnTitle: string): number {
let res: number = 0;
for (let char of columnTitle) {
res = res * 26 + char.charCodeAt(0) - 64;
}
return res;
}
class Solution {
public:
int titleToNumber(string columnTitle) {
int res = 0;
for (char c : columnTitle) {
res = res * 26 + (c - 'A' + 1);
}
return res;
}
};
func titleToNumber(columnTitle string) int {
res := 0
for _, c := range columnTitle {
res = res*26 + int(c-'A'+1)
}
return res
}