LC-13 罗马数字转整数

Easy

根据对应的罗马数字代表的整数值+六个特例,进行转化。

https://leetcode-cn.com/problems/roman-to-integer/submissions/

/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function (s) {
  let res = 0;
  if (s.includes("IV") || s.includes("IX")) res -= 2;
  if (s.includes("XL") || s.includes("XC")) res -= 20;
  if (s.includes("CD") || s.includes("CM")) res -= 200;
  for (let c of s) {
    switch (c) {
      case "I": res += 1;break;
      case "V": res += 5;break;
      case "X": res += 10;break;
      case "L": res += 50;break;
      case "C": res += 100;break;
      case "D": res += 500;break;
      case "M": res += 1000;break
    }
  }
  return res
};