13. Roman to Integer
Description
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 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. 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: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
分析和方案
这是把罗马数字转化成阿拉伯数字的题,罗马数字在计算时,要看当前字符的右侧,比右侧小的,当前值要去减去,比如CM是900. 所以在计算时,需要读下一位才能判断当前值如何使用。
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var map = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
var x = 0
for(var i = 0; i < s.length; i++){
if(s[i+1] && map[s[i]] < map[s[i+1]]){
x = x - map[s[i]]
}else{
x += map[s[i]]
}
}
return x
};
最快
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var runningTotal = 0;
const conversionFactor = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
let currentIndex = 0;
while (currentIndex < s.length - 1) {
let currentLetter = s[currentIndex];
let nextLetter = s[currentIndex + 1];
if (conversionFactor[currentLetter] >= conversionFactor[nextLetter]) {
runningTotal += conversionFactor[currentLetter];
} else {
runningTotal -= conversionFactor[currentLetter];
}
currentIndex += 1;
}
runningTotal += conversionFactor[s[s.length - 1]];
return runningTotal;
};
第二
var romanToInt = function(s) {
let m = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
if (s.length == 0) return 0
let sum = 0
for(let i = 1; i < s.length; i ++){
let pre = m[s[i-1]]
sum += pre
if (m[s[i]] > pre){
sum -= pre * 2
}
}
return sum + m[s[s.length-1]]
}
这里加完之后,判断一下再减两倍的逻辑挺有意思的,这样可以节省一个else判断,不确定是否对速度有提升。
第三
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var symbols = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
var values = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
const len = s.length;
var anwer = 0;
for(var i = len - 1; i >= 0; i--){
if(i !== 0 ){
var checkTwo = s[i-1]+ s[i];
var indexDouble = symbols.indexOf(checkTwo);
if(indexDouble >= 0){
anwer += values[indexDouble];
i--;
} else {
anwer += values[symbols.indexOf(s[i])];
}
} else {
anwer += values[symbols.indexOf(s[i])];
}
}
return anwer;
};