当前位置:网站首页>【刷题记录】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 的长度。
- 空间复杂度:。
运行结果

总结
边栏推荐
- GO 单元测试
- Equivalent domain name
- If you merge cells by Excel, export excelutils
- 减半行情会不会来?有何投资机会?2020-03-11
- 35岁以上的测试/开发程序员职业生涯走向,是职场转折点吗?
- Nombre minimal d'échanges
- MOF customized materials | bimetallic CuNi MOF nano materials | core-shell structure [email protected] Nanocomposites | zif-8/ poly
- AE如何制作星云粒子特效
- C语言进阶——自定义类型:结构体 枚举 联合
- Qiyue supplies cumof nanocrystals loaded with methylene blue | femof nanosheets grown in situ on foam nickel | oxide nanowires /zif MOFs sugar gourd like Composites
猜你喜欢

Unveiling secrets of matrixcube 101 - functions and architecture of matrixcube

Wrong again, byte alignment and the use of pragma pack
[email protected] Support) | uio-66/coso composite | zif-67 nanocrystalline sur"/>Copper sulfide nanoparticles /zif-8 Composites( [email protected] Support) | uio-66/coso composite | zif-67 nanocrystalline sur
![[error record /selectpicker] the display position of dropdown menu is offset](/img/92/78787b33b941f09d753f0f747f8c72.png)
[error record /selectpicker] the display position of dropdown menu is offset

Opencv:06 morphology

2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元

深度梳理:机器学习建模调参方法总结

全球金融危机来袭,如何科学理性投资?2020-03-17

Uio-66 | fe3o4/cu3 (BTC) 2 metal organic framework (MOF) nanocomposites supported on silver nanoparticles | nagdf4:yb, er upconversion nanoparticles @zif-8

Label ball problem
随机推荐
Which domestic API tool is better? It turned out to be it
Method of converting video format to ffmpeg and exporting GIF dynamic graph
Label ball problem
Lazy to the bone, I am too lazy to write articles in CSDN. I write articles based on selenium simulation
R语言--Cox模型校准曲线原理(一)数据来源
2022年最新吉林建筑安全员模拟题库及答案
Useeffect summary
全球金融危机来袭,如何科学理性投资?2020-03-17
通货收缩的市场何时反转?我们该如何操作?2020-03-13
MOF customized materials | bimetallic CuNi MOF nano materials | core-shell structure [email protected] Nanocomposites | zif-8/ poly
大气非等晕效应
Basic database operations in MySQL
Equivalent domain name
Cloud health management system based on STM32 (using Alibaba cloud Internet of things platform)
Brief analysis of circuit fault
可视化ETL工具Kettle概念、安装及实战案例
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
OpenCV:06形态学
力扣413-等差数列划分——动态规划
Investment logic in market "uncertainty" 2020-03-18