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; } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 09:43:59