Skip to content

Instantly share code, notes, and snippets.

@hungneox
Last active July 18, 2021 13:30
Show Gist options
  • Save hungneox/8fcff676c2b9b8d02fdb641bc07b44ee to your computer and use it in GitHub Desktop.
Save hungneox/8fcff676c2b9b8d02fdb641bc07b44ee to your computer and use it in GitHub Desktop.
Excel Sheet Column Title
# https://leetcode.com/problems/excel-sheet-column-title
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
"""
Success
Details
Runtime: 28 ms, faster than 77.90% of Python3 online submissions for Excel Sheet Column Title.
Memory Usage: 14.1 MB, less than 70.63% of Python3 online submissions for Excel Sheet Column Title.
"""
if columnNumber <= 26:
return chr(64+columnNumber)
left = columnNumber // 26
right = columnNumber % 26
if (right == 0):
left = (left -1) if left > 1 else 1
return self.convertToTitle(left) + self.convertToTitle(26)
return self.convertToTitle(left) + self.convertToTitle(right)
@hungneox
Copy link
Author

hungneox commented Jul 18, 2021

(26^0*x)+(26^1*1)+(26^2*1)+(26^3*1) = AAAA (where x = 1)
'ZZ' = 702 = ((26^0*26) + (26^1*26)) = 26 + 676 
'AB' = 26^1*1 + 2 = 28
'BY' = 26*1*2 + 25 = 77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment