본문 바로가기
Algorithm/LeetCode

[LeetCode] 13 | Roman to Integer

by 밤초록 2022. 1. 28.
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

 

반응형

댓글