https://leetcode.com/problems/roman-to-integer/
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
#II -> 2
#MCMXCIV -> M / CM / XC / IV
res = 0
#딕셔너리 사용 (값:인덱스)
romanint = {
'I' : 1,
'V' : 5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
}
#s의 길이가 0일떄까지
for i in range(len(s)):
if(i<len(s)-1 and romanint[s[i]] < romanint[s[i+1]]):
res -= romanint[s[i]]
else:
res += romanint[s[i]]
return res

'PROGRAMMING > [Python] LeetCode' 카테고리의 다른 글
| [멘토링] sliding window, two sum (0) | 2026.05.06 |
|---|---|
| [LeetCode/Python] 1. Two Sum (0) | 2026.05.05 |