LeetCode #13
题目(英)
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(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.
题目(中)
罗马数字包含以下七种字符: 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.
solution 1: 每次比两位,然后判断下次跳几位
解法1的核心主要是利用循环进行一位一位的判断,每次都比较当前位i
和下一位i+1
,要是前者大的话就把当前位i
对应的数字加上,要是后者大的话,就加上这两位的差。
注意:
- 因为我们是判断
i
和i+1
,所以我们要注意越界的问题,当i = len(s) - 1
是会报错,所以要将这个情况单独进行考虑 (即elif部分)。 - 针对两种情况,我们的i每次变化的值是有区别的,一个是 +1,一个是 +2。所以我们这里的循环只能用while。如果用for的话会出问题,比如
for i in range(len(s)):
,那么即使我们后面 +2,我们的代码仍会强制把 [0:len(s)-1] 中的每一个整数都遍历一遍,会报错。 - 我们也可以用
min()
函数来解决i+1
越界的问题,具体改动为if i + 1 <= len(s): if numeral_map[s[i]] >= numeral_map[s[min(i+1,len(s)-1)]]
确保s不会有比len(s) - 1
还要大的索引。
具体代码如下:
class Solution:
def romanToInt(self, s: str) -> int:
numeral_map = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
result = 0
i = 0
while i < len(s):
if i + 1 < len(s):
if numeral_map[s[i]] >= numeral_map[s[i+1]]:
result += numeral_map[s[i]]
i += 1
else:
result += numeral_map[s[i+1]] - numeral_map[s[i]]
i += 2
elif i == len(s)-1:
result += numeral_map[s[i]]
i += 1
return result
solution 2:
解法2的核心是将两位一起读的情况直接写进字典中,我们每次遍历两位,要是能在字典中找到对应的值就直接读出来,找不到就默认读一位。
注意:
- 我们创建字典是要把所有的情况都列出来,但是因为enumerate是每一位都会遍历的,所以我们把两位数的值都减1,比如
IV
本来是4,但是我们存的是3。所以IV
的话,是先读I
,也就是1 ,接着到V
,读到的是IV
也就是3,所以结果是1+3=4。 - enumerate(枚举)是一个特殊的函数,他每次会有两个
output
: 一个index
也就是这里的i
,另一个是value
也就是这里的n
。 dict.get( )
里面是字典的一个特殊函数,有两个参数,第一个是要找的值也就是这里的s[max(i-1, 0):i+1]
,另一个是默认值d[n]
,也就是当第一个值找不到的时候我们不会报错而是直接取第二个值。- 为了避免上一个解法的越界问题,这个解法是每次比较当前位
i
和前一位i-1
,因为s[max(i-1, 0):i+1]
是包头不包尾的,之所以嵌套了一个max()
函数是为了避免这个解法中i-1
越界的问题。(即当 i = 0 时,i-1 不会报错,因为小于0了)
具体代码如下:
class Solution:
def romanToInt(self, s: str) -> int:
d = {'I':1, 'IV':3, 'V':5, 'IX':8, 'X':10, 'XL':30, 'L':50, 'XC':80, 'C':100, 'CD':300, 'D':500, 'CM':800, 'M':1000}
return sum(d.get(s[max(i-1, 0):i+1], d[n]) for i, n in enumerate(s))
总结
- 这种对应的关系,翻译转换之类的(比如:密文…),都可以优先考虑字典来进行数据的对应。
- 在一次遍历过程中,我们要比较多位数字,但是有可能出现越界的情况时,我们可以灵活应用
min()
和max()
来规避风险。 - enumerate()函数遍历过程中会依次生成每次的
index
和value
. 比如当input
为 III 时 ,结果分别是:i=1,n=‘I’ ; i=2, n=‘I’ ; i=3, n=‘I’。