LeetCode171——Excel Sheet Column Number

  很简单的一道题,输出excel中字母对应的数字。但是由于自己的程序思想还是很差,代码很长。

class Solution
{
public:
    int titleToNumber(string s)
    {
        int ans = 0;
        int tmp = 1;
        auto len = s.size();

        if (len == 1)
        {
            return letterToNumber(s[0]);
        }

        for (auto i = 0; i != len; ++ i)
        {
            ans += letterToNumber(s[i]) * pow(26,(len - tmp));
            tmp ++;
        }
        ans += letterToNumber(s[len - tmp]);
        return ans;
    }
    int letterToNumber(char p)
    {
        int ans = 0;
        switch(p)
        {
            case ‘A‘:
                ans = 1;
                break;
            case ‘B‘:
                ans = 2;
                break;
            case ‘C‘:
                ans = 3;
                break;
            case ‘D‘:
                ans = 4;
                break;
            case ‘E‘:
                ans = 5;
                break;
            case ‘F‘:
                ans = 6;
                break;
            case ‘G‘:
                ans = 7;
                break;
            case ‘H‘:
                ans = 8;
                break;
            case ‘I‘:
                ans = 9;
                break;
            case ‘J‘:
                ans = 10;
                break;
            case ‘K‘:
                ans = 11;
                break;
            case ‘L‘:
                ans = 12;
                break;
            case ‘M‘:
                ans = 13;
                break;
            case ‘N‘:
                ans = 14;
                break;
            case ‘O‘:
                ans = 15;
                break;
            case ‘P‘:
                ans = 16;
                break;
            case ‘Q‘:
                ans = 17;
                break;
            case ‘R‘:
                ans = 18;
                break;
            case ‘S‘:
                ans = 19;
                break;
            case ‘T‘:
                ans = 20;
                break;
            case ‘U‘:
                ans = 21;
                break;
            case ‘V‘:
                ans = 22;
                break;
            case ‘W‘:
                ans = 23;
                break;
            case ‘X‘:
                ans = 24;
                break;
            case ‘Y‘:
                ans = 25;
                break;
            case ‘Z‘:
                ans = 26;
                break;
            default:
                break;
        }
        return ans;
    }
};

看了别人的代码之后,深深感觉自己的弱小。他们利用了ascii码极大的减少了switch case 的繁琐。

int titleToNumber(string s) {

    int len = s.length(), sum=0;

    for(int i=0;i<len;i++)
        sum += (int(s[i]-‘A‘+1))*pow(26,len-1-i);

    return sum;
}

短短几行代码就将这个问题解决了,看来学习之路还很漫长!加油!!!!!!!

时间: 2024-10-14 12:21:53

LeetCode171——Excel Sheet Column Number的相关文章

leetcode171 Excel Sheet Column Number

题意: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 思路:找规律,挺简单的···可是小地方错了一些搞了半天···心塞 代码: int titleToNumber(string s) { int n = s.length(); int ans = 0; for(int i=0;i<n;i++) { int temp = (int)s[i]-64; ans += temp * func(n-i); } retu

171. Excel Sheet Column Number

Excel Sheet Column Number 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 static publi

2015.5.18——Excel Sheet Column Number

Excel Sheet Column Number 本题收获: 1.对于字符串中字母转为ASIIC码:string s ;res = s[i]-'A'; 这个res就是数字s[i]-'A'是对ASIIC的操作. 2.对于进制的转换:利用for循环 ,%,/,数组在完成. 3.2中的数组都是从左到右的,在此题中没有按照高地位 题目: elated to question Excel Sheet Column Title Given a column title as appear in an Ex

[LeetCode] Excel Sheet Column Number

Excel Sheet Column Number 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 解题思路: 看成是进制转

LeetCode Excel Sheet Column Number 表列数

题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA"=26*2+1=53,"CBA"=26*26*3+26*2+1.相当于321(十进制),那么就是10*10*3+10*2+1,说第3条式子,C在第3位,在第1位时就是1~26的值了,也就是3,在第2位时就是将3*26了,再第3位时就是将3*26*26了:同理B就是2*26啦,A就是1

Excel Sheet Column Number &amp; Excel Sheet Column Title

1. Excel Sheet Column Number 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 1 class S

171. Excel Sheet Column Number(C++)

171. Excel Sheet Column Number 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 题目大意: 相

leetcode——171 Excel Sheet Column Number (表格字母与整数的映射)

Excel Sheet Column Number 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 Hide Tags: M

leetcode Excel Sheet Column Number python

Excel Sheet Column Number 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 python code