Algorithm/LeetCode 문제 풀이

[LeetCode 13. Roman to Integer] Easy (19주차 5/10, 19/150)

공놀이나하여보세 2019. 5. 12. 09:45

class Solution:
    def romanToInt(self, s: str) -> int:
        sum = 0
        for i in range(len(s)):
            if(s[i]=='I'):
                if(i + 1 < len(s) and (s[i+1] == 'V' or s[i+1] == 'X')):
                    sum -=1
                else:
                    sum += 1
            elif(s[i] == 'V'):
                sum += 5
            elif(s[i] == 'X'):
                if(i + 1 < len(s) and (s[i+1] == 'C' or s[i+1] == 'L')):
                    sum -=10
                else:
                    sum += 10
            elif(s[i] == 'L'):
                sum += 50
            elif(s[i] == 'C'):
                if(i + 1 < len(s) and (s[i+1] == 'M' or s[i+1] == 'D')):
                    sum -=100
                else:
                    sum += 100
            elif(s[i] == 'D'):
                sum += 500
            elif(s[i] == 'M'):
                sum += 1000
                
        print(sum)
        return sum