Leetcode题解之 —— 罗马数字转整数

思路


暴力解法 + Map

  • 两个Map
  • 指针移动距离

题解


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
const romanToIntMap = new Map([
['I', 1],
['V', 5],
['X', 10],
['L', 50],
['C', 100],
['D', 500],
['M', 1000],
]);
const specialIntMap = new Map([
['IV', 4],
['IX', 9],
['XL', 40],
['XC', 90],
['CD', 400],
['CM', 900],
]);

if (s.length === 1) {
return romanToIntMap.get(s);
}

let [count, result] = [0, 0];

while (count < s.length) {
if (specialIntMap.has(s[count] + s[count + 1])) {
result += specialIntMap.get(s[count] + s[count + 1]);
count += 2;
} else {
result += romanToIntMap.get(s[count]);
count += 1;
}
}

return result;
};