letecode [171] - Excel Sheet Column Number

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

题目大意

   根据给定规则,1对应A,2对应B,...,26对应Z,27对应AZ,...。求正整数对应的字符串。

理  解:

   遍历字符串,每获得一个字符,计算它对应的整数,求和。

代 码 C++:

class Solution {
public:
    int titleToNumber(string s) {
        int len = s.length();
        int i=0,sum=0,n=len;
        while(i<n){
            sum += (s[i]-64)*pow(26,len-1);
            i++;
            len--;
        }
        return sum;
    }
};

运行结果:

   执行用时 :8 ms, 在所有C++提交中击败了88.58%的用户

   内存消耗 :8.2 MB, 在所有C++提交中击败了26.03%的用户

posted @ 2019-06-11 16:30  lpomeloz  阅读(89)  评论(0编辑  收藏  举报