# 168. Excel Sheet Column Title

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/excel-sheet-column-title/

> 内容描述

```
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 
    ...
Example 1:

Input: 1
Output: "A"
Example 2:

Input: 28
Output: "AB"
Example 3:

Input: 701
Output: "ZY"
```

## 解题方案

> 思路 1
******- 时间复杂度: O(log(N, 26))******- 空间复杂度: O(1)******

beats 82.58%

```python
class Solution:
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = ''
        while n :
            res = chr(ord('A') + (n - 1) % 26) + res
            n = (n - 1) // 26
        return res
```

> 思路 2
******- 时间复杂度: O(log(N, 26))******- 空间复杂度: O(1)******

其实也可以不用 (n-1) 去取mod，只要注意当remainder为0的时候就直接取'Z'就可以了

beats 99.24%

```python
class Solution:
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = ''
        mod = n # in  case that n is 0 or less
        while True:
            if mod <= 0:
                break
            mod, remainder = divmod(mod, 26)
            if remainder == 0:
                mod -= 1
                res = 'Z' + res
            else:
                res = chr(64 + remainder) + res # ord(A) = 65
        return res
```




























