[LeetCode] 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
给定一个整数,求出其在Excel对应的字母标题,利用while循环,先对整数取余(n - 1) % 26,找到数字与字母的关系,计算出最后一位的字母,然后对整数取商,计算下一位。最后将得到的结果字符串反转即可。
class Solution { public: string convertToTitle(int n) { string s; int tmp = 0; while (n) { n = n - 1; tmp = n % 26; s += ('A' + tmp); n /= 26; } int left = 0, right = s.size() - 1; while (left <= right) swap(s[left++], s[right--]); return s; } }; // 0 ms