标签:
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
class Solution {
public:
int titleToNumber(string s) {
int result=0;
for(int i=0;i<s.size();++i)
{
result=result*26+(s[i]-64);
}
return result;
}
};Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
class Solution {
public:
string convertToTitle(int n) {
string resultSrt;
while (n != 0)
{//string和容器比较类似
int tmp = (n-1) % 26;
resultSrt.push_back('A'+tmp);//并不单纯的是一个26进制数
n = (n-1) / 26;
}
reverse(resultSrt.begin(), resultSrt.end());
return resultSrt;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
leetCode(48):Excel Sheet Column Number And Excel Sheet Column Title
标签:
原文地址:http://blog.csdn.net/walker19900515/article/details/47035347