Roman to Integer - LeetCode
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 ones 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 Explanation: III = 3. Example 2:
Input: s = “LVIII” Output: 58 Explanation: L = 50, V= 5, III = 3. Example 3:
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].
class Solution:
def romanToInt(self, s: str) -> int:
romanDict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
characterIndex = 0
result = 0
while characterIndex < len(s):
if characterIndex == len(s) - 1:
result += romanDict[s[characterIndex]]
return result
if s[characterIndex] == 'I' and (s[characterIndex + 1] == 'V' or s[characterIndex + 1] == 'X'):
result += romanDict[s[characterIndex + 1]] - romanDict[s[characterIndex]]
characterIndex += 2
elif s[characterIndex] == 'X' and (s[characterIndex + 1] == 'L' or s[characterIndex + 1] == 'C'):
result += romanDict[s[characterIndex + 1]] - romanDict[s[characterIndex]]
characterIndex += 2
elif s[characterIndex] == 'C' and (s[characterIndex + 1] == 'D' or s[characterIndex + 1] == 'M'):
result += romanDict[s[characterIndex + 1]] - romanDict[s[characterIndex]]
characterIndex += 2
else:
result += romanDict[s[characterIndex]]
characterIndex += 1
return result
以上代码可以解决问题,但是也有一些问题。现在优化一下:
- 减少代码冗余:代码中有许多重复的部分,特别是检查是否满足特殊情况的部分。我们可以简化这些检查。
- 从后往前遍历:一个常见的技巧是从罗马数字的末尾开始遍历。这样我们可以先加上最后一个数字,然后看前一个数字是否小于当前数字。如果是,我们就减去它。
class Solution:
def romanToInt(self, s: str) -> int:
romanDict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
preNum = 0
total = 0
for character in s[::-1]:
value = romanDict[character]
if value < preNum:
total -= value
else:
total += value
preNum = value
return total
class Solution {
public int romanToInt(String s) {
Map<Character, Integer> romanDict = new HashMap<>();
romanDict.put('I', 1);
romanDict.put('V', 5);
romanDict.put('X', 10);
romanDict.put('L', 50);
romanDict.put('C', 100);
romanDict.put('D', 500);
romanDict.put('M', 1000);
int total = 0;
int preNum = 0;
int value = 0;
for (int i = s.length() - 1; i >= 0; i--) {
value = romanDict.get(s.charAt(i));
if (value < preNum){
total -= value;
}else {
total += value;
}
preNum = value;
}
return total;
}
}
class Solution {
public:
int romanToInt(string s) {
std::unordered_map<char, int> romanDict = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int prevValue = 0;
int total = 0;
for (int i = s.size() - 1; i >= 0; i--) {
char symbol = s[i];
int value = romanDict[symbol];
if (value < prevValue) {
total -= value;
} else {
total += value;
}
prevValue = value;
}
return total;
}
};