프로그래밍/LeetCode
[LeetCode][Python3] 171. Excel Sheet Column Number
snoopybox
2018. 11. 3. 21:45
Problem :
https://leetcode.com/problems/excel-sheet-column-number/
My Solution :
class Solution:
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
ans = 0
for i, c in enumerate(s[::-1]):
ans += (ord(c)-64)*(26**i)
return ans
Comment :
그냥 단순한 26진수 문제이다. 위 처럼 뒤에서부터 앞으로 올라가는 방식이 있고, 아래처럼 앞에서부터 뒤로 내려가도 된다.
My Solution 2:
class Solution:
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
ans = 0
for c in s:
ans *= 26
ans += ord(c)-64
return ans