[LeedCode OJ]#171 Excel Sheet Column Number

来源:互联网 发布:相思相见知何日 编辑:程序博客网 时间:2024/04/30 17:44
 【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】

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

题意:
给出一些字母组成的字符串,输出对应的数字

思路:
看成26进制,变成进制换算的题

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;    }};


0 0
原创粉丝点击