본문 바로가기
공부하며놀자/프로그래밍

[leetcode] 13. Roman to Integer

by 테너토너 2021. 1. 16.

13. Roman to Integer

Easy

28473880Add to ListShare

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

 

Example 1:

Input: s = "III" Output: 3

Example 2:

Input: s = "IV" Output: 4

Example 3:

Input: s = "IX" Output: 9

Example 4:

Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

 

이제와서 보니 제약 사항을 제대로 안 봤었네.

우선 1차적으로 동작하도록만.

class Solution:
    def romanToInt(self, s: str) -> int:
        s = s.upper()
        
        answer = 0

        dic = {'MMM':3000, 'MM':2000, 'CM':900, 'M':1000, 'CD':400, 'D':500, 'CCC':300,'CC':200, 'XC':90,'C':100, 'XL':40, 'L':50, 'XXX':30,'XX':20, 'IX':9, 'X':10,  'IV':4,'V':5, 'III':3,'II':2,'I':1 }
        for key in dic:
            if s.find(key) != -1:
                print(key)
                answer += dic[key]
                s = s.replace(key, "")
                continue
        
        return answer

쓰레기 수준.

Success

Details 

Runtime: 64 ms, faster than 11.86% of Python3 online submissions for Roman to Integer.

Memory Usage: 14.5 MB, less than 26.43% of Python3 online submissions for Roman to Integer.

 

 

///

다른 솔루션을 참고하여 다시 작성.

class Solution:
    def romanToInt(self, s: str) -> int:
        s = s.upper()
        
        answer = 0

        dic = {'M':1000, 'D':500,'C':100, 'L':50, 'X':10,'V':5,'I':1 }
        previous = 0
        current = 0
        
        for e in s:
            current = dic[e]
            
            answer += current
            
            if previous < current :
                answer = answer - previous*2
            
            previous = current
            
            
        
        return answer

Details 

Runtime: 40 ms, faster than 93.09% of Python3 online submissions for Roman to Integer.

Memory Usage: 14.2 MB, less than 57.19% of Python3 online submissions for Roman to Integer.

Next challenges:

Integer to Roman

Show off your acceptance:

 

 

Time SubmittedStatusRuntimeMemoryLanguage

01/16/2021 22:37 Accepted 40 ms 14.2 MB python3
01/16/2021 02:11 Accepted 64 ms 14.5 MB python3
반응형

댓글