Problem Solving

[leetcode - python] Excel Sheet

Dev_en 2022. 7. 20. 00:15

문제 설명

https://leetcode.com/problems/excel-sheet-column-number/submissions/

 

Excel Sheet Column Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

  • 난이도 - Easy

A~Z가 1~26열을 나타내고, 27 이상은 AA와 같이 알파벳의 조합으로 표현될 때, 주어진 알파벳 조합이 몇열인지 구하는 문제


관련 개념

문자열, 아스키코드


접근 방법

26^(자릿수) + 알파벳에 해당하는 열

을 아스키 코드를 이용해 문자로 숫자로 변환해 계산


답안 코드

Runtime: 52 ms (47.28%)
Memory Usage: 14 MB (10.48%)
class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        result = 0
        location = len(columnTitle) - 1
        for c in columnTitle:
            result += 26 ** location * (ord(c) - ord('A') + 1)
            location -= 1
        return result