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) { char turn[26] = {‘Z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘,‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘}; string result; int i = 0; while(n) { i = n % 26; result.push_back(turn[i]); n = n / 26; if(i == 0) n = n - 1; } return result.assign(result.rbegin(), result.rend()); } };
class Solution { public: string convertToTitle(int n) { string ret = ""; while(n) { ret = (char)((n-1)%26+‘A‘) + ret; n = (n-1)/26; } return ret; } };
原文:http://www.cnblogs.com/dylqt/p/4919399.html