当前位置:网站首页>13. 罗马数字转整数
13. 罗马数字转整数
2022-06-22 20:51:00 【前端粉刷匠】
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900
方法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;
};
边栏推荐
- Seriously, the hang up of the kotlin collaboration process is not so mysterious (principle)
- Atcoder abc256 full problem solution (interval merging template, matrix fast power optimization DP, line segment tree...)
- What are the methods of software stress testing and how to select a software stress testing organization?
- 2021-04-16
- 2021-04-14
- 阻止别人使用浏览器调试
- 【Kubernetes 系列】Kubernetes 概述
- JSBridge
- 2021-07-27
- 2020-12-04
猜你喜欢

Pycharm configuring remote connection server development environment

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

Some shaders in AB package do not trigger the callback of ipreprocessshaders
Learn redis with you (11) -- redis distributed lock

Generate detailed API and parameters of QR code using qrcodejs2

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

5 minutes to quickly launch web applications and APIs (vercel)

MySQL master-slave synchronization and its basic process of database and table division

2021-08-26

The mental process and understanding of visual project code design
随机推荐
SOA Service Oriented Architecture
Mysql database DQL query operation
保证数据库和缓存的一致性
2020-12-20
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
Why do you perform performance tests before the software goes online? How to find a software performance testing organization
2020-12-20
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
2021-08-22
2021-03-06
One case of SQL performance degradation caused by modifying implicit parameters
Spark SQL Generic Load/Save Functions(2.4.3)
2020-12-20
Using the hbuilder x editor to install a solution for terminal window plug-ins that are not responding
Several ways of redis persistence -- deeply parsing RDB
Codeup longest palindrome substring
A group of K overturned linked lists [disassembly / overturning / assembly of linked lists]
node-fetch下载文件
AutoCAD - five annotation shortcuts
为 localStorage 添加过期时间