Skip to content

168. Excel Sheet Column Title 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
class Solution {
 public:
  string convertToTitle(int n) {
    return n == 0 ? ""
                  : convertToTitle((n - 1) / 26) + (char)('A' + ((n - 1) % 26));
  }
};
1
2
3
4
5
class Solution {
  public String convertToTitle(int n) {
    return n == 0 ? "" : convertToTitle((n - 1) / 26) + (char) ('A' + ((n - 1) % 26));
  }
}
1
2
3
4
5
class Solution:
  def convertToTitle(self, n: int) -> str:
    return (self.convertToTitle((n - 1) // 26) + chr(ord('A') + (n - 1) % 26)
            if n
            else '')