使用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. 使用 Python 获取 Windows 聚焦图片

    Windows 聚焦图片会定期更新,拿来做壁纸不错,它的目录是: %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw ...

  2. windows系统mysql-5.7官方绿色版zip包安装教程

    准备 下载页面:https://dev.mysql.com/downloads/mysql/ 点击 Download 按钮下载zip包到本地,解压(以我本地的解压路径是 D:\db\mysql-5.7 ...

  3. 来看一下Java中“-”与equeals的区别

    简介: == ==是比较两个变量的值,如果是基本数据类型,那么就是比较的基本数据的大小值 情况一 int a=1; int b=1; System.out.println(a==b); 以上图中:== ...

  4. cc.isChildClassOf 判断两个类的继承关系

    使用 cc.isChildClassOf 来判断两个类的继承关系: var Texture = cc.Class(); var Texture2D = cc.Class({ extends: Text ...

  5. 03篇ELK日志系统——升级版集群之ELK日志系统整合springboot项目

    [ 前言:整个ELK日志系统已经搭建好了,接下来的流程就是: springboot项目中的logback日志配置通过tcp传输,把springboot项目中所有日志数据传到————>logsta ...

  6. The 2016 ACM-ICPC Asia China-Final L World Cup(深搜+回溯 暴力求解)

    题目分析: 对于A,B,C,D四支队伍,两两之间进行一场比赛,获胜得3分,平局得1分,失败不得分,现在对给出的四个队伍的得分,判断能否满足得到这种分数,且方案唯一输出yes,不唯一输出no,不可能则输 ...

  7. 网传英特尔酷睿第十代桌面处理器(Comet Lake 14nm)规格

    自从农企(AMD)2016年开始崛起时,牙膏厂(英特尔)就开始发力,陆续两代推出性价比颇高的桌面处理器, 第八代.第九代酷睿桌面处理器相当的给力,而第十代酷睿桌面处理器会很猛啊,据传从酷睿i3到酷睿i ...

  8. Comet OJ - Contest #10 C.鱼跃龙门

    传送门 题意: 求最小的\(x\),满足\(\frac{x(x+1)}{2}\% n=0,n\leq 10^{12}\). 多组数据,\(T\leq 100\). 思路: 直接考虑模运算似乎涉及到二次 ...

  9. django 权限设置-菜单显示

    问题:在用户登录后,如何只显示出用户权限的菜单呢?需要设置显示菜单权限 1.为了显示菜单,需要在models权限上添加is_menu(手动判断是否是查看)的icon(图标字符串) 在rbac中录入另一 ...

  10. 02-linux-换源-ui方式

    换软件源 使用清华的软件源. Ubuntu 的 ui 界面操作^换源 System setting -> Software & update -> Download from -& ...