Easy-to-understand C++ solution


  • 1
    C

    beause there is no 'zero' ,so we should make the 'Z'(26) corresponding to 'zero',so if the mod is zero,it means 'Z' and meanwhile n=n/26-1

        class Solution {
    public:
        string convertToTitle(int n) {
            string ans;
            while (n>0)
            {
                if (n%26==0)
                {
                    ans='Z'+ans;
                    n=n/26-1;
                }
                else
                {
                    ans=(char)('A'+n%26-1)+ans;
                    n=n/26;
                }
            }
            return ans;
            
        }
    };

Log in to reply
 

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