当前位置:网站首页>13. Roman numeral to integer
13. Roman numeral to integer
2022-06-22 23:39:00 【Front end plasterer】
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1.12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
C Can be placed in D (500) and M (1000) Left side , To express 400 and 900
Method 1:
function romanToInt(s) {
let map = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
let result = 0;
for (let i = 0; i < s.length; i++) {
if(map[s[i]] < map[s[i+1]]){
result -= map[s[i]]
}else{
result += map[s[i]]
}
}
return result;
};
边栏推荐
猜你喜欢

2021-05-02

swagger2 使用方法

企业数字化不是各自发展,而是全面SaaS化推进

Synchronization circuit and cross clock domain circuit design 2 -- cross clock domain transmission (FIFO) of multi bit signals
![[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board](/img/3c/717b4a481bfff1d37e3c271f37a7d6.png)
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board

OJ每日一练——过滤多余的空格

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

tp5.1上传excel文件并读取其内容

IPV4的未来替代品!一文读懂IPV6的优势特点和地址类型
随机推荐
LeakCanary 源码详解(2)
Safe and reliable! Tianyi cloud data security management platform passed the evaluation
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
Tp5.1 upload excel file and read its contents
Spark SQL Start(2.4.3)
OJ daily practice - filter extra spaces
Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience
Array and string offset access syntax with curly braces is no longer support
OJ每日一练——验证子串
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
好东西要分享啦
DCC888 :SSA (static single assignment form)
OJ每日一练——找第一个只出现一次的字符
2021-08-26
OJ每日一练——过滤多余的空格
2020-12-20
【Kubernetes 系列】Kubernetes 概述
js图片分辨率压缩
swagger2 使用方法