Leetcode 168 Solution

This article provides solution to leetcode question 168 (excel-sheet-column-title)

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

Solution

class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        ans = []

        v = columnNumber
        while v > 0:
            if v <= 26:
                ans.append(chr(ord('A') + v - 1))
                break
            else:
                ans.append(chr(ord('A') + (v - 1) % 26))

                v = (v - 1) // 26

        return "".join(reversed(ans))