# Solution with Explanation

• The idea behind this problem is as follows:

Consider getting the characters for 28: It's 26^1 * (first Character) + 26^0 * (second Character), which in this case is 26*'A' + 1*'B' . Therefore to get the last character mod by 26. Then to get the character prior to that divide by 26 then mod by 26 and so on till what remains is zero.

``````string convertToTitle(int n) {
string res="";
while(n>0){
res=char('A'+(n-1)%26)+res;
n=(n-1)/26;
}
return res;
}``````

• Could you please explain why n = (n-1)/26 instead of n=n/26?

• I am wrong at this point for the first try. My understanding is because 'A' corresponds to 1 instead of 0, the A-Z correspond to 1-26, our base-26 system should be 0-25 . It is really a tricky pitfall.

• I kinda get it eventually. You can think of the -1 part as changing from 1-based index to 0-based index.

• Just got trapped with the "minus one" issue, and your explanation of "changing from 1-based to 0-based system" is very helpful. Thanks!

• @larryliu0104 Could you please explain the concept of base-26 system?
I am so confused.

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