Excel Sheet Column Title
题目:
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
cpp:
class Solution { public: string convertToTitle(int n) { string result; int a; while (n > 0) { a = (n - 1) % 26; n = (n - 1) / 26; result.push_back(a + 'A'); } reverse(result.begin(), result.end()); return result; } };
python:
class Solution(object): def convertToTitle(self, n): s = [] while n > 0: a = (n - 1) % 26 s.append(chr(a+65)) n = int((n - 1) / 26) return ''.join(s[::-1])

浙公网安备 33010602011771号