Leetcode # 168. Excel Sheet Column Title

Problem

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

1 → A
2 → B
3 → C

26 → Z
27 = 1 * 26 + 1AA
28 = 1 * 26 + 2AB

Solution

Time Complexity: O(log(columnNumber))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def convertToTitle(self, columnNumber: int) -> str:
    col_num, col_title = columnNumber, ""
    while col_num > 0:
      col_title = chr((col_num := col_num - 1) % 26 + 65) + col_title
      col_num //= 26
    return col_title

 

Last Updated on 2023/08/23 by A1go

目錄
Bitnami