https://leetcode.com/problems/roman-to-integer/
Solution 1
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
let total = 0;
for (let i = 0; i < s.length; i++) {
if (values[s[i]] < values[s[i + 1]]) {
total += values[s[i + 1]] - values[s[i]];
i++;
} else {
total += values[s[i]];
}
}
return total;
};
const values = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
Runtime: 235 ms, faster than 40.69% of JavaScript online submissions for Roman to Integer.
Memory Usage: 47.1 MB, less than 67.53% of JavaScript online submissions for Roman to Integer.