使用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. instr函数的用法

    1.定义 instr函数返回要截取的字符串在源字符串中的位置 语法如下:  instr( string1, string2 [, start_position [, nth_appearance ] ...

  2. 学习Linq之前必须要了解的扩展方法

    本文主要以下面几个方面来详细讲解扩展方法:在C#3.0之前没有扩展方法的状态(或者你不会使用不知道扩展方法的时候).扩展方法的语法及怎么使用.怎么正确的使用扩展方法: 一.首先说一下在C#3.0之前没 ...

  3. Jenkins的使用(二)-------MSBuild

    六.构建 左边菜单栏    Manage Jenkins --->ManagePlugins--->可选插件,然后搜索 MSBuild,并安装 添加ssh插件后新增了两种 1.使用MSBu ...

  4. EF core的原生SQL查询以及用EF core进行分页查询遇到的问题

    在用.net core进行数据库访问,需要处理一些比较复杂的查询,就不得不用原生的SQL查询了,然而EF Core 和EF6 的原生sql查询存在很大的差异. 在EF6中我们用SqlQuery和Exe ...

  5. 极速体验docker容器健康

    本文目是体验docker容器的健康检查功能,以体验为主不涉及开发,与开发相关的内容会在后面的文章细说. 关于容器健康检查 考虑这样的情况:docker环境中,springboot应用的容器还在,但已无 ...

  6. 【代码笔记】Web-CSS-CSS组合选择符

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  7. SVG跟随父级DIV自适应

    后台返回过来的是这样的SVG标签 <svg width="100%" height="100%" version="1.1" xmln ...

  8. APICloud项目纪要

    一.页面之间的传递参数通过pageParam传递参数: api.openWin({ name: 'ware', url: './ware.html', pageParam: { wareId: 'w1 ...

  9. FPM十一:点击POPUP显示明细

    沿接着前面的Search和List.在LIST中点击一列,弹出窗口显示明细. 1.list中定义事件: METHOD if_fpm_guibb_list~get_definition. DATA:gt ...

  10. 结对编程作业(java实现)

    项目成员:罗海屏.郑晓婷 一 .Github项目地址:https://github.com/ting9500/GNIT_Second 二.PSP表格 PSP2.1 Personal Software ...