0%

excel-sheet-column-number

Excel Sheet Column Number – LeetCode 171

Problem

Description

Given a column title as appear in an Excel sheet, return its corresponding column number.

Example

A -> 1
B -> 2
C -> 3

Z -> 26
AA -> 27
AB -> 28

Answer

Original

Code

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int titleToNumber(string s) {
unsigned sum = 0;
for(char &c: s){
sum *= 26;
sum += c - 'A' + 1;
}
return sum;
}
};

思路

没什么思路,水题。时间复杂度$O(n)$,空间复杂度$O(1)$。
耗时$6$ ms,排名$90.91\%$

Better

思路

没有看到更好的思路。