当前位置:网站首页>【刷题记录】13. 罗马数字转整数
【刷题记录】13. 罗马数字转整数
2022-07-17 17:29:00 【InfoQ】
一、题目描述
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
输入: s = "III"
输出: 3
输入: s = "IV"
输出: 4
输入: s = "IX"
输出: 9
输入: s = "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.
输入: s = "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.
- 1 <= s.length <= 15
- s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M')
- 题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内
- 题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。
- IL 和 IM 这样的例子并不符合题目要求,49 应该写作 XLIX,999 应该写作 CMXCIX 。
- 关于罗马数字的详尽书写规则,可以参考 罗马数字 -Mathematics。
二、思路分析
- 罗马数字由
I,V,X,L,C,D,M 构成;
- 当小值在大值的左边,则减小值,如
IV=5-1=4;
- 当小值在大值的右边,则加小值,如
VI=5+1=6;组合起来:如果小值放在大值的左边,就做减法,否则为加法。
三、代码实现
class Solution {
Map<String, Integer> map = new HashMap<>() {{
put("M", 1000);
put("D", 500);
put("C", 100);
put("L", 50);
put("X", 10);
put("V", 5);
put("I", 1);
}};
public int romanToInt(String s) {
int sum = 0;
int res = map.get(s.substring(0, 1));
for (int i = 1; i < s.length(); i++) {
int num = map.get(s.substring(i, i + 1));
if (res < num) {
sum -= res;
} else {
sum += res;
}
res = num;
}
sum += res;
return sum;
}
}
复杂度分析
- 时间复杂度:,其中
n 是字符串 s 的长度。
- 空间复杂度:。
运行结果

总结
边栏推荐
- The difference and use between get request and post request
- C语言进阶——自定义类型:结构体 枚举 联合
- Method of converting video format to ffmpeg and exporting GIF dynamic graph
- 回顾2008年金融危机,做长期主义投资者 2020-03-19
- Mysql的知识梳理
- Unveiling secrets of matrixcube 101 - functions and architecture of matrixcube
- Gold nanoparticles modified mil-101 framework material (AuNPs / mil-101) / loaded cof-tppa-1 (Au NPs / cof-tppa-1) | Qiyue reagent
- Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
- Ultrasonic sensor (ch101 & ch201) - I
- JS operation string string string
猜你喜欢

XML文件解析

松下A6伺服驱动器外部绝对值光栅尺全闭环参数设置

XML建模(简单易学)

Investment logic in market "uncertainty" 2020-03-18

Opencv:06 morphology

VMware导入ova/ovf虚拟机文件

Differences between get requests and post requests and usage examples

标签球问题

2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790

CMOS开关学习(一)
随机推荐
How to invest scientifically and rationally when the global financial crisis strikes? 2020-03-17
Impulse function, step function, ramp function and impulse response
Visual ETL tool kettle concept, installation and practical cases
Unveiling secrets of matrixcube 101 - functions and architecture of matrixcube
收益风险比:投资机会最重要指标 2020-03-14
LeetCode 0117. 填充每个节点的下一个右侧节点指针 II
Method of converting video format to ffmpeg and exporting GIF dynamic graph
Brief analysis of circuit fault
【Pygame 学习笔记】5.rect对象的碰撞检测
R language -- principle of Cox model calibration curve (I) data source
The difference and use between get request and post request
Label ball problem
运维小白成长记—架构第6周
Nombre minimal d'échanges
運維小白成長記—架構第6周
Chitosan coated pcn224 nanoparticles | metal organic skeleton fe-mil-88nh2 | nickel based MOF material (Ni MOF / NF)
Redis logical cluster creation
关于TCP/IP协议漏洞的安全措施
Growth of operation and maintenance Xiaobai - week 6 of Architecture
R语言--Cox模型校准曲线原理(一)数据来源