Short "0ms" C++ solution


  • 11
    D
    class Solution {
    public:
        string convertToTitle(int n) {
            string result;
            while (n) {
                result.push_back('A' + (n - 1) % 26), n = (n - 1) / 26;    
            }
            reverse(result.begin(), result.end());
            return result;
        }
    };

  • 3
    S

    or we can use

    result = (char)('A' + (n - 1) % 26) + result;
    

    so new characters are added in front of result each time, avoiding using reverse at last


Log in to reply
 

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