码迷,mamicode.com
首页 > 其他好文 > 详细

Excel Sheet Column Number

时间:2015-07-06 10:29:40      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

详细解释参见与其相对的另一道题的分析,这里的处理过程是她的逆过程而已另一道题
代码如下:

class Solution {
public:
    int titleToNumber(string s) {
        string::reverse_iterator iter1, iter2;
        iter1 = s.rbegin();
        iter2 = s.rend();
        int output = 0;
        for(; iter1 != s.rend(); iter1++) {
            if(*iter1 == ‘Z‘) {
                if(output == 0) {
                    *iter1 = ‘0‘;
                    output = 1;            
                }else {
                    *iter1 = ‘A‘;
                    output = 1;
                }
                continue;
            }

            if(*iter1 == ‘Y‘) {
                if(output == 1) {
                    *iter1 = ‘0‘;
                    output = 1;
                }else {
                    output = 0;
                }
                continue;
            }

            if(output == 1) {
                *iter1 = (char)(*iter1 + 1);
                output = 0;
            }
        }

        int result = 0;
        int factor = 1;
        for(iter1 = s.rbegin(); iter1 != s.rend(); iter1++) {
            if(*iter1 != ‘0‘) {
                result = (*iter1 - 64) * factor + result;
            }
            factor = factor * 26;
        }
        if(output == 1)
            result = result + factor;

        return result;

    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

Excel Sheet Column Number

标签:

原文地址:http://blog.csdn.net/guanzhongshan/article/details/46766885

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!