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"

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Mathematics

Starter code: class Solution:
    def convertToTitle(self, n: int) -> str:
        

Solutions:
class Solution:

	def convertToTitle(self, n):
		ans = ''
		a = 0
		while n > 0:
			if a > 0:
				n = n // 26
			p = n % 26
			if p == 0:
				p = 26
			ans += chr(64 + p)
			n -= p
			a += 1
		return ans[::-1]


class Solution:

	def convertToTitle(self, n):
		return '' if n == 0 else self.convertToTitle(int((n - 1) / 26)) + chr((n - 1) % 26 + ord('A'))


class Solution:

	def convertToTitle(self, num):
		res = []
		while num > 0:
			res.append(chr(ord('A') + (num - 1) % 26))
			num = (num - 1) // 26
		res.reverse()
		return ''.join(res)


class Solution:

	def convertToTitle(self, n):
		alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		c = 1
		clm = ''
		while n > 0:
			if n % 26 > 0:
				clm = alph[n % 26 - 1] + clm
				n = (n - n % 26) // 26
			elif n % 26 == 0:
				clm = alph[25] + clm
				n = (n - 26) // 26
		return clm


class Solution:

	def convertToTitle(self, n):
		res = ''
		while n != 0:
			tmp = (n - 1) % 26
			c = chr(ord('A') + tmp)
			res = c + res
			n = (n - 1) // 26
		return res


class Solution:

	def convertToTitle(self, n):
		if n <= 26:
			return chr(n + 64)
		out = ''
		r = n
		while r >= 1:
			out += chr((r - 1) % 26 + 65)
			r = (r - 1) // 26
		return out[::-1]


class Solution:

	def convertToTitle(self, n):
		letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		result = ''
		while n:
			n -= 1
			result = letters[n % 26] + result
			n //= 26
		return result


class Solution:

	def convertToTitle(self, n):
		ret = []
		while n > 0:
			n -= 1
			tmp = n % 26
			n = int(n / 26)
			ret.append(chr(ord('A') + tmp))
		return ''.join(reversed(ret))


class Solution:

	def convertToTitle(self, n):
		dict_alpha = {0: 'Z', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y'}
		output = []
		while n > 0:
			output.append(dict_alpha[n % 26])
			n = (n - 1) // 26
		output.reverse()
		return ''.join(output)