使用eval,特别处理6个case

 var romanToInt = function (s) {
const map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
var fns = '0' + s.replace('CM', '+900'). replace('CD', '+400').
replace('XL': '+40').
replace('XC', '+90').
replace('IV', '+4').
replace('IX', '+9').
replace(/[C-X]/g, function (c) {
return "+" + map[c]
})
return eval(fns)
};
console.log(intToRoman("MCMXCIV"))

不使用eval

var romanToInt = function (s) {
const map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
const specialMap = {
CM: 900,
CD: 400,
XL: 40,
XC: 90,
IV: 4,
IX: 9
}
var sum = 0
for (var i = 0; i < s.length; i++) {
var c = s[i];
var more = c + s[i + 1];
if (specialMap[more]) {
sum += specialMap[more];
i += 1;
continue
} else {
sum += map[c]
}
}
return sum
};

方法3, 我们可以观察一下 下一个字符代码的数值是否比当前的大,大则减去。

var romanToInt = function (s) {
const map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
var sum = 0
for (var i = 0; i < s.length; i++) {
var c = s[i];
var curr = map[c]
var after = s[i + 1];
if (map[after] > curr) {
sum -= curr
} else {
sum += curr
}
}
return sum
};

leetcode 13. Integer to Roman的更多相关文章

  1. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  2. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  4. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  5. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  6. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  7. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  8. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  9. 【JAVA、C++】LeetCode 012 Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. excel中使用统计列中的值在其他列出现的次数

    excel中使用统计一列的中值在其他列出现的次数 =COUNTIFS($J$:$J$,K2) 解释下 $J$2 J列中的第二行到 $J$373 J列的373行  范围内 查找 k列的第二行的值 出现的 ...

  2. Vector线程安全,ArrayList非线程安全

    http://baijiahao.baidu.com/s?id=1638844080997170869&wfr=spider&for=pc Vector线程安全,ArrayList非线 ...

  3. Blend Brush介绍

    原文:Blend Brush介绍 这篇文章会介绍 Blend怎么设置Brush 全局画刷 1)blend面板的介绍 这5个rectangle分别对应 blend中的 5个设置 1 设置无颜色 2 设置 ...

  4. WPF DispatcherTimer一些个人看法 (原发布 csdn 2017-04-25 19:12:22)

    wpf中的DispatcherTimer基本用法,本文不在叙述.主要写一些不同的,来提醒自己不要再犯同样错误. 前几天写代码时发现.当在非UI线程创建DispatcherTimer实例时,程序无法进入 ...

  5. Queue接口分析:add和offer区别,remove和poll方法到底啥区别

    Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...

  6. C# 与 Java 的一些差异

    如果你是 Java 开发人员,则可以在 Xamarin 平台上充分利用你的技能和现有代码,同时获得 C# 的代码重用优势.你会发现 C# 语法与 Java 语法非常相似,这两种语言提供非常类似的功能. ...

  7. 活动任务出现bug

    之前做的一个活动任务发现一个bug,是以前和离职同事一起对逻辑的时候没有考虑到的,配置活动的时候应该要先查询下,如果这个产品线上在这段时间已经配置了并且上线了,则不能在做活动处理了,否则就和前面的活动 ...

  8. C++中的Mat, const Mat, Mat &,Mat &, const Mat &的区别

    Mat, copy传递,不会改变外部变量的Mat. Mat &, reference传递,函数内部修改将会改变外部. const Mat, copy传递,在函数内,不会被修改,也不会影响到外部 ...

  9. android studio学习----偏好设置

    主要找到设置的界面:Files->Settings , android studio里面没有preferences 1.字体大小设置 进入后你也许发现字体大小或者样式不符合你的习惯,比如我是觉得 ...

  10. 竟然有人在群里谈交钱培训PI。。。。等哥哥有时间,断了你们的财路

    PI是工具,很不错的工具.统一管理接口,这点对大公司来说还是有必要的.而且PI的日志记录很详细,用的好的话,绝对物超所值.