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数字和字符串的对应。
简单题,找到计算方法摹拟1下就好了。
n = 26^n * a1 + 26^(n⑴) * a2 + ...... + 26^0 *a(n+1)
class Solution { public: string convertToTitle(int n) { string res; while(n) { int c = (n % 26) - 1; if(c == ⑴) c = 25; res = char('A' + c) + res; n--; n /= 26; } return res; } };看到优美解法:
return n == 0 ? "" : convertToTitle(n / 26) + (char) (--n % 26 + 'A');