Skip to content

Latest commit

 

History

History
65 lines (55 loc) · 989 Bytes

File metadata and controls

65 lines (55 loc) · 989 Bytes

168. 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"

Solutions (Python)

1. Solution

class Solution:
    def convertToTitle(self, n: int) -> str:
        ret = ""
        while n > 0:
            n -= 1
            ret = chr(n % 26 + 65) + ret
            n //= 26
        return ret

Solutions (Ruby)

1. Solution

# @param {Integer} n
# @return {String}
def convert_to_title(n)
    ret = ""

    while n > 0
        n -= 1
        ret = (n % 26 + 65).chr + ret
        n /= 26
    end

    return ret
end