171. Excel Sheet Column Number

来源:互联网 发布:powerdesigner mac 编辑:程序博客网 时间:2024/06/08 16:23

原题

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 -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 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

原创粉丝点击