Problem
Given an integercolumnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
Examples
Constraints
- 1 <= columnNumber <= 2^31 - 1
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 168 with 18 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 168 # by problem number
lcpy gen -s excel_sheet_column_title # by problem name
columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Input: columnNumber = 1
Output: "A"
Input: columnNumber = 28
Output: "AB"
Input: columnNumber = 701
Output: "ZY"
class Solution:
# Time: O(log_26 n)
# Space: O(log_26 n)
def convert_to_title(self, column_number: int) -> str:
result: list[str] = []
while column_number > 0:
column_number -= 1 # shift 1-indexed alphabet to 0-indexed
result.append(chr(ord("A") + column_number % 26))
column_number //= 26
return "".join(reversed(result))
| Time | Space |
|---|---|
| O(log_26 n) | O(log_26 n) |