13 | Roman to Integer
https://leetcode.com/problems/roman-to-integer/




작성 코드
class Solution:
def romanToInt(self, s: str) -> int:
sum = 0
s = list(s)
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
while s:
if len(s) > 1 and roman_dict[s[-1]] > roman_dict[s[-2]]:
sum += roman_dict[s.pop()] - roman_dict[s.pop()]
else:
sum += roman_dict[s.pop()]
return sum
반응형
'Algorithm > LeetCode' 카테고리의 다른 글
| [LeetCode] 695 | Max Area of Island (0) | 2022.03.17 |
|---|---|
| [LeetCode] 1822 | Sign of the Product of an Array (0) | 2022.03.15 |
| [LeetCode] 191 | Number of 1 Bits (0) | 2022.03.10 |
| [LeetCode] 14 | Longest Common Prefix (0) | 2022.02.22 |
| [LeetCode] 561 | Array Partition I (0) | 2022.02.17 |
댓글