标签:
Q:Related to question Excel Sheet Column Title
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
这道题不难,关键在于看出是类似于“二十六进制”的计算。一开始我不太确定AZ之后是什么,后来就建了一个excel文档尝试了一下发现是BA,哈哈,也是够蠢的。举个例子吧,BAD = 26^2*(‘B’-‘A‘+1)+26^1*(‘A‘-‘A‘+1)+26^0*(‘D‘-‘A‘+1)
下面直接贴上代码:
class Solution { public: int titleToNumber(string s) { int i = 0; int result=0; int len = s.length(); for (; i < len; i++){ result += pow(26, len-1-i)*(s[i] - 'A' + 1); } return result; } };
[LeetCode]Excel Sheet Column Number
标签:
原文地址:http://blog.csdn.net/kaitankedemao/article/details/43601667