Short and non-recursive C++ solution with a note about the algorithm trick.

  • 5

    The solution is similar to converting a number to different base. Only trick is when building the string the characters need to get inserted in the front rather that appended to the back. Here's the code:

    string convertToTitle(int n) {
        string r;
        for (; n > 0; n /= 26) r = (char)(--n % 26 + 'A') + r;
        return r;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.