题目描述:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999
罗马数字转换为阿拉伯数字的注意点有这么几点:
1. 对应意义'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000
2. 左大于等于右简单加法,左小于右简单减法,优先级从我的理解来看是减法大于加法。
python
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} if s =='0': return false else: res = 0 for i in range(len(s)): if i == 0 or dict[s[i]] <= dict[s[i-1]]: res += dict[s[i]] else: res += dict[s[i]] - 2*dict[s[i-1]] if res in range(1,4000): return res else: return false