Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A2 -> B3 -> C…26 -> Z27 -> AA28 -> AB
解题思路:
十进制转化成26进制。注意对于string类型来说,并不存在string=char+string类型操作符,只有string=string+char操作符。因此,,可以用栈来存储低位,然后出栈。
另外一个需要注意的是(n-1)%26+’A’,要n-1哈。
class Solution {public:string convertToTitle(int n) {string result="";stack<char> s;while(n!=0){s.push((n-1) % 26 + 'A');n = (n-1) / 26;}while(!s.empty()){result += s.top();s.pop();}return result;}};
梦想,并不奢侈,只要勇敢地迈出第一步。