当前位置:网站首页>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;
};
边栏推荐
- Web Caching Technology
- Tp5.1 upload excel file and read its contents
- Spark SQL Start(2.4.3)
- SOA Service Oriented Architecture
- Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction
- ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”
- Uniapp modifies array properties, and the view is not updated
- 多种方案实现图片的懒加载
- 优化——线性规划
- 对 cookie 的添加/获取和删除
猜你喜欢

保证数据库和缓存的一致性

Enterprise digitalization is not a separate development, but a comprehensive SaaS promotion

2021-04-14

在Word中自定义多级列表样式

c语言---17 函数简介

Spark RDD Programming Guide(2.4.3)

xml转义字符对照表

2021-08-26
![[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

企业数字化不是各自发展,而是全面SaaS化推进
随机推荐
mysql主从同步及其分库分表基本流程
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
tp5.1上传excel文件并读取其内容
Considerations for using redisson to operate distributed queues
优化——线性规划
Asynchronous FIFO
China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
Canvas generate Poster
Common operations of sourcetree version management
tp5.1解决跨域
在Word中自定义多级列表样式
Learning the interpretable representation of quantum entanglement, the depth generation model can be directly applied to other physical systems
Programmers' choice of taking private jobs and part-time jobs
Unity: use ray to detect objects
OJ每日一练——找第一个只出现一次的字符
OJ daily practice - delete word suffixes
Tp5.1 solving cross domain problems
OJ每日一练——过滤多余的空格
Dip1000,1 of D
剑指 Offer 07. 重建二叉树