7/28/2020

[LeetCode] 168. Excel Sheet Column Title

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

Time Complexity = O ( Log N )

class Solution:
    def convertToTitle(self, n: int) -> str:
        letters = [chr(i) for i in range(ord('A'),ord('Z')+1)]
        
        result = ""
        
        while n > 0:
            # use n - 1 because letters is zero-based array
            i = (n-1) % 26
            
            result += letters[i]
            n = (n-1) // 26
        
        return result[::-1]

No comments:

Post a Comment