반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 171 | Excel Sheet Column Number | Python 본문
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
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 148 | Sort List | Python (0) | 2022.02.24 |
---|---|
[LeetCode] 133 | Clone Graph | Python (0) | 2022.02.23 |
[LeetCode] 169 | Majority Element | Python (0) | 2022.02.21 |
[LeetCode] 402 | Remove K Digits | Python (0) | 2022.02.18 |
[LeetCode] 39 | Combination Sum | Python (0) | 2022.02.17 |
Comments