美文网首页LeetCode每日一题
Python实现-LeetCode(0013)-罗马数字转整数(

Python实现-LeetCode(0013)-罗马数字转整数(

作者: 复苏的兵马俑 | 来源:发表于2020-03-14 15:30 被阅读0次

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。

字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

  例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II

  通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

  I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
  C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
  给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。

示例 1:

输入: "III"
输出: 3

示例 2:

输入: "IV"
输出: 4

示例 3:

输入: "IX"
输出: 9

示例 4:

输入: "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.

示例 5:

输入: "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.

题解思路1(正则匹配法,待优化):
LeetCode中提交执行结果-执行用时:376 ms,内存消耗:13.5 MB。

class Solution:
    def romanToInt(self, s: str) -> int:
        import re
        roman_dict = {
            'IV': 4,
            'IX': 9,
            'XL': 40,
            'XC': 90,
            'CD': 400,
            'CM': 900,
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100,
            'D': 500,
            'M': 1000
        }
        num = 0
        while s:
            for key in roman_dict:
                if re.match(key,s):
                    num += roman_dict[key]
                    if len(key) == 1:
                        s = s[1:]
                    if len(key) == 2:
                        s = s[2:]
                        break
                else:
                    continue
        return num

print('“III”对应的整数为:{}'.format(Solution().romanToInt('III')))
print('“IV”对应的整数为:{}'.format(Solution().romanToInt('IV')))
print('“IX”对应的整数为:{}'.format(Solution().romanToInt('IX')))
print('“LVIII”对应的整数为:{}'.format(Solution().romanToInt('LVIII')))
print('“MCMXCIV”对应的整数为:{}'.format(Solution().romanToInt('MCMXCIV')))

运行结果:

“III”对应的整数为:3
“IV”对应的整数为:4
“IX”对应的整数为:9
“LVIII”对应的整数为:58
“MCMXCIV”对应的整数为:1994

题解思路2:
LeetCode中提交执行结果-执行用时:52 ms,内存消耗:13.3 MB。

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

print('“III”对应的整数为:{}'.format(Solution().romanToInt('III')))
print('“IV”对应的整数为:{}'.format(Solution().romanToInt('IV')))
print('“IX”对应的整数为:{}'.format(Solution().romanToInt('IX')))
print('“LVIII”对应的整数为:{}'.format(Solution().romanToInt('LVIII')))
print('“MCMXCIV”对应的整数为:{}'.format(Solution().romanToInt('MCMXCIV')))

运行结果:

“III”对应的整数为:3
“IV”对应的整数为:4
“IX”对应的整数为:9
“LVIII”对应的整数为:58
“MCMXCIV”对应的整数为:1994

题解思路3:
LeetCode中提交执行结果-执行用时:60 ms,内存消耗:13.4 MB。

class Solution:
    def romanToInt(self, s: str) -> int:
        roman_dict = {
            'I': 1,
            'IV': -1,
            'V': 5,
            'IX': -1,
            'X': 10,
            'XL': -10,
            'L': 50,
            'XC': -10,
            'C': 100,
            'CD': -100,
            'D': 500,
            'CM': -100,
            'M': 1000
        }
        return sum(roman_dict.get(s[i: i + 2], roman_dict[s[i]]) for i in range(len(s)))

print('“III”对应的整数为:{}'.format(Solution().romanToInt('III')))
print('“IV”对应的整数为:{}'.format(Solution().romanToInt('IV')))
print('“IX”对应的整数为:{}'.format(Solution().romanToInt('IX')))
print('“LVIII”对应的整数为:{}'.format(Solution().romanToInt('LVIII')))
print('“MCMXCIV”对应的整数为:{}'.format(Solution().romanToInt('MCMXCIV')))

运行结果:

“III”对应的整数为:3
“IV”对应的整数为:4
“IX”对应的整数为:9
“LVIII”对应的整数为:58
“MCMXCIV”对应的整数为:1994

题解思路4:
LeetCode中提交执行结果-执行用时:88 ms,内存消耗:13.6 MB。

class Solution:
    def romanToInt(self, s: str) -> int:
        roman_dict = {
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100,
            'D': 500,
            'M': 1000
        }
        result = 0
        prev_num = 0
        for i in reversed(s):
            num = roman_dict.get(i)
            if num < prev_num:
                result -= num
            else:
                result += num
                prev_num = num
        return result

print('“III”对应的整数为:{}'.format(Solution().romanToInt('III')))
print('“IV”对应的整数为:{}'.format(Solution().romanToInt('IV')))
print('“IX”对应的整数为:{}'.format(Solution().romanToInt('IX')))
print('“LVIII”对应的整数为:{}'.format(Solution().romanToInt('LVIII')))
print('“MCMXCIV”对应的整数为:{}'.format(Solution().romanToInt('MCMXCIV')))

运行结果:

“III”对应的整数为:3
“IV”对应的整数为:4
“IX”对应的整数为:9
“LVIII”对应的整数为:58
“MCMXCIV”对应的整数为:1994

相关文章

网友评论

    本文标题:Python实现-LeetCode(0013)-罗马数字转整数(

    本文链接:https://www.haomeiwen.com/subject/ujyqshtx.html