작심삼일

[LeetCode] 171 | Excel Sheet Column Number | Python 본문

스터디/코테

[LeetCode] 171 | Excel Sheet Column Number | Python

yun_s 2022. 2. 22. 15:09
728x90
반응형

문제 링크: https://leetcode.com/problems/excel-sheet-column-number/


문제

Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

조건

  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

내 풀이

ColumnTitle을 27진수로 보고 풀면 된다.


코드

class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        ans = 0
        N = len(columnTitle)
        for n in range(N-1, -1, -1):
            ans += 26**n * (ord(columnTitle[N-n-1])-ord('A')+1)
            
        return ans
728x90
반응형
Comments