LeetCode – Excel Sheet Column Title solution in Java

Tags: , ,

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example:

1 -> A
2 -> B
3 -> C

26 -> Z
27 -> AA
28 -> AB

To convert a number from a system with base m to Decimal number system, we can use the following formula:

xyz = x * m^2 + y * m^1 + z * m^0.

For instance, the binary number 1010 = 1 * 2^3 + 1 * 2^1 = 10. 

Now to convert a number N in decimal system to a system with base m, we just do the following until N is 0.

For this problem, we need to transfer a number into a system with base 26.  A normal system starts with 0.

As this system is from 1 to 26. We can map it from 0 to 25. Then plus 1.

The mapping is :

So we have to use (n -  1) / 26 in the update.

The following the implementation in Java.