Clean and easy-understand C++ Solution


  • 1
    S
    string convertToTitle(int n) {
        string res;
        while(n)
        {
            res += char('A' + ((n - 1) % 26));
            n -= ((n - 1) % 26 + 1);
            n /= 26;
        }
        reverse(res.begin(), res.end());
        return res;
    }

  • 0
    G

    Line #6: "n -= ((n - 1) % 26 + 1);" is unnecessary as line#7 will automatically remove the remainder.


Log in to reply
 

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