题目描述
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
题目大意:不解释
样例
Example 1:
Input: “A”
Output: 1
Example 2:
Input: “AB”
Output: 28
Example 3:
Input: “ZY”
Output: 701
python解法
class Solution:
def titleToNumber(self, s: str) -> int:
eng = {chr(i): i-64 for i in range(65,91)}
sum = 0
for i,val in zip(range(len(s)), reversed(s)):
sum += eng[val] * (26**i)
return sum
Runtime: 44 ms, faster than 22.90% of Python3 online submissions for Excel Sheet Column Number.
Memory Usage: 13.9 MB, less than 9.09% of Python3 online submissions for Excel Sheet Column Number.
题后反思:无
C语言解法
int titleToNumber(char * s){
int len = strlen(s);
int sum = 0;
for (int i=0;i<len;i++)
{
sum += (s[len-i-1]-64)*(int)(pow(26,i));
}
return sum;
}
Runtime: 4 ms, faster than 63.93% of C online submissions for Excel Sheet Column Number.
Memory Usage: 7 MB, less than 33.33% of C online submissions for Excel Sheet Column Number.
题后反思:无
文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步