[LeetCode][Python3] 171. Excel Sheet Column Number
2018. 11. 3. 21:45 |
프로그래밍/LeetCode
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
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 191. Number of 1 Bits (0) | 2018.11.04 |
---|---|
[LeetCode][Python3] 190. Reverse Bits (0) | 2018.11.04 |
[LeetCode][Python3] 189. Rotate Array (0) | 2018.11.03 |
[LeetCode][Python3] 172. Factorial Trailing Zeroes (0) | 2018.11.03 |
[LeetCode][Python3] 122. Best Time to Buy and Sell Stock II (0) | 2018.11.03 |
[LeetCode][Python3] 88. Merge Sorted Array (0) | 2018.11.03 |
[LeetCode][Python3] 118. Pascal's Triangle (0) | 2018.10.17 |
[LeetCode][Python3] 125. Valid Palindrome (0) | 2018.10.17 |
최근에 달린 댓글 최근에 달린 댓글