使用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. 用友UI层获取机构的方法

    U层: UFIDA.U9.UI.PDHelper.PDContext.Current.OrgIDPDContext.Current.OrgRef.CodeColumn

  2. mvc5 源码解析2-1:mvchandler的执行

    上一节说在urlroutingmodule中mvchandler 映射到httpcontext上,那mvchandler又是怎么执行的呢? (1).httpruntime 从isapiruntime  ...

  3. English--音标拼读

    English|音标拼读 音标拼读主要内容是,如何使用音标进行单词的拼读,并且会有相应的语音现象,最关键的还是自己多加练习,多听~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识 ...

  4. .net Redis分布式锁,Dictionary,ConcurrentDictionary 介绍

    1.背景 在计算机世界里,对于锁大家并不陌生,在现代所有的语言中几乎都提供了语言级别锁的实现,为什么我们的程序有时候会这么依赖锁呢?这个问题还是要从计算机的发展说起,随着计算机硬件的不断升级,多核cp ...

  5. ES6 模块(八)

    在node环境中运行需要使用babel命令将ES6代码转换为ES5代码再执行相关文件使用命令直接将src目录下所有ES6代码转换ES5代码到dist目录下: $ babel src --out-dir ...

  6. AIX运维常用命令

    目前传统的磁盘管理仍有不足:如果下Unix系统中的存储容量需要扩展,文件系统就必须停止运行,然后通过重构分区的手段来进行分区和文件系统的扩容.一般采用的方法是先备份该文件系统并删除其所在的分区,然后重 ...

  7. Ninja使用Visual Studio(cl.exe)构建

    目录 Ninja基本步骤 Ninja在VS2015下的问题和解决 Ninja命令行参数 Ninja错误的调用了gcc Ninja基本步骤 Ninja的作用是加速构建,最初目的是替代make,现在Win ...

  8. sudo: /usr/lib/sudo/sudoers.so must be only be writable by owner

    因为某种原因,手动给usr文件夹改了权限,之后我自己这个账户(非root)就不能运行sudo命令,提示"sudo: /usr/lib/sudo/sudoers.so must be only ...

  9. python 和 R 语言中的等差数列

    等差数列的通项公式:an = a0 + n*d. 数学上 n 是可以取遍整个整个正整数集的,在现实中,n  是有范围的. 1.R 语言用 seq()  函数产生等差数列: 2.python 中 ran ...

  10. MarkDown中如何加入上标和下标

    上标 使用<sup></sup>标签包裹的部分就是上标,例如:A<sup>T</sup> 显示效果就是 AT . 下标 使用<sub>< ...