13. Roman to Integer {Easy}

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, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

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

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

My silly solution:

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        value = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
        symbol = ['M', 'CM', 'D','CD', 'C','XC', 'L','XL','X','IX', 'V', 'IV','I'];
        num = 0
        while(len(s) > 0):
            if s[0:2] in symbol:
                num += value[symbol.index(s[0:2])]
                s = s[2:]
            else:
                num += value[symbol.index(s[0:1])]
                s = s[1:]
        return num

swarooprs94's solution: 对于 input,每一位做判断,如果比后一位小,就是减法,否则是加法。

class Solution:
        def romanToInt(self, s):
            dictionary = {
                    'I': 1,
                    'V': 5,
                    'X': 10,
                    'L': 50,
                    'C': 100,
                    'D': 500,
                    'M': 1000
                    }
            total_sum = 0
            for i, item in enumerate(s):
                if i != len(s) - 1 and dictionary.get(item) < dictionary.get(s[i+1]):
                    total_sum -= dictionary.get(item)
                else:
                    total_sum += dictionary.get(item)
            return total_sum
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,150评论 0 10
  • 先列出问题: mysql.sock 这个文件在你的pc上的地址可能不同,不过不影响,通过提示找到相应的目录就行...
    忘了呼吸的那只猫阅读 46,301评论 0 2
  • 拖了一周的的稿子,再不写完大概就要绝交了…… 上次的问题是:“产品经理喜欢跟什么样的运营合作?” 我的第一直觉回复...
    墨韵书香阅读 3,991评论 0 0
  • 《弹窗》。2020年,重庆。贤盯着面前空无一物的人形皮囊,冷汗顺着鬓角滴下来。瞬间,人工视网膜上投射出一个人影,那...
    小九风云阅读 929评论 0 0
  • 自控力不等于无欲无求,也不等于枯燥无趣。 我理解的自控力是指在什么时间点,什么环境下,做该做的事情,并且高效完成。...
    三月花开了阅读 1,911评论 0 1