[LeedCode OJ]#171 Excel Sheet Column Number

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:[email protected]

题目链接:https://leetcode.com/problems/excel-sheet-column-number/

题意:
给定A~Z,分别代表1~26,AA-27,AB-28等等,现在给定一个由字母组成的字符串,要求将其转换为数字

思路:
运用进制的思想,转换成求进制的思路即可

class Solution
{
public:
    int titleToNumber(string s)
    {
        int ans = 0;
        int len = s.length(),i,j;
        for(i = 0; i<len; i++)
        {
            int cnt = len-1-i;
            int a = s[i]-'A'+1,q = 1;
            while(cnt--)
                q*=26;
            ans=ans+a*q;
        }
        return ans;
    }
};


你可能感兴趣的:(leedcode)