标签:
详细解释参见与其相对的另一道题的分析,这里的处理过程是她的逆过程而已另一道题
代码如下:
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;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/guanzhongshan/article/details/46766885