原题
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
代码实现
public int TitleToNumber(string s)
{
char[] chs= {'A','B','C','D','E','F','G','H','I','J','K',
'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
Dictionary<char,int> dict = new Dictionary<char,int>();
for(int i=0; i<chs.Length;i++) dict.Add(chs[i],i+1);
int scnt = s.Length, rtnsum=0;
for (int i = scnt - 1,j=0; i >= 0; i--,j++)
rtnsum += dict[s[i]] * (int)System.Math.Pow(26, j);
return rtnsum;
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp