코딩 테스트 및 알고리즘/leetcode for google

leetcode easy : Excel Sheet Column Title

띠리링구 2023. 1. 12. 01:23

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

 

Excel Sheet Column Title - LeetCode

Excel Sheet Column Title - Given an integer 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 ...   Example 1: Input: columnNumber = 1 Output: "A" Example 2

leetcode.com

 

짱짱 쉬워보이는데 은근히 까다롭다.

규칙을 잘 찾아야되는데 일반적인 26진수라고 생각하고 접근하면 잘 안된다.

1-indexed이기 때문에 이걸 꼭 고려하고 -1씩 해주는걸 생각해야된다.

class Solution:
    def convertToTitle(self, x: int) -> str:
        return "" if not x else self.convertToTitle((x - 1) // 26) + chr(ord('A') + (x - 1) % 26)