当前位置:网站首页>[record of question brushing] 13 Roman numeral to integer
[record of question brushing] 13 Roman numeral to integer
2022-07-19 13:15:00 【InfoQ】
One 、 Title Description
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
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.
Input : s = "III"
Output : 3
Input : s = "IV"
Output : 4
Input : s = "IX"
Output : 9
Input : s = "LVIII"
Output : 58
explain : L = 50, V= 5, III = 3.
Input : s = "MCMXCIV"
Output : 1994
explain : M = 1000, CM = 900, XC = 90, IV = 4.
- 1 <= s.length <= 15
- s Characters only ('I', 'V', 'X', 'L', 'C', 'D', 'M')
- Topic data assurance s It's a valid Roman number , And it means that the integer is in the range [1, 3999] Inside
- The test cases given in the title all conform to the Roman numeral writing rules , There will be no straddle, etc .
- IL and IM Such an example does not meet the requirements of the title ,49 You should write XLIX,999 You should write CMXCIX .
- Detailed rules for writing Roman numerals , You can refer to Rome digital -Mathematics.
Two 、 Thought analysis
- Roman numerals by
I,V,X,L,C,D,M constitute ;
- When the small value is to the left of the large value , Then decrease the value , Such as
IV=5-1=4;
- When the small value is to the right of the large value , Then add a small value , Such as
VI=5+1=6;combined : If the small value is placed to the left of the large value , Just subtract , Otherwise it's addition.
3、 ... and 、 Code implementation
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;
}
}
Complexity analysis
- Time complexity :, among
n Is string s The length of .
- Spatial complexity :.
Running results

summary
边栏推荐
- 【腾讯蓝鲸】第七届 7·24 运维日节日祝福送上~ 快来许愿~
- Reg of sequential logic and combinatorial logic
- Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
- C语言进阶——自定义类型:结构体 枚举 联合
- 光大期货网上开户安全吗?有没有开户指引?
- Li Kou 70 - climbing stairs - Dynamic Planning
- MatrixCube揭秘 101——MatrixCube的功能与架构
- Ultrasonic sensor (ch101 & ch201) - Ⅱ
- 如何在MFC中添加一个线程
- Metal organic framework material / polymer composite zif-8/p (TDA co HDA) | zinc oxide [email protected] (FE) composite nan
猜你喜欢

LeetCode 0117. 填充每个节点的下一个右侧节点指针 II

Panasonic A6 servo driver external absolute value grating ruler full closed loop parameter setting

Att & CK actual combat series - red team actual combat (-)

力扣198-213 打家劫舍Ⅰ、Ⅱ——动态规划

MatrixCube揭秘 101——MatrixCube的功能与架构
[email protected] Cobalt iron bimetallic organic skeleton cox/mil-100 (FE) | [email protected]

【腾讯蓝鲸】第七届 7·24 运维日节日祝福送上~ 快来许愿~

XML建模(简单易学)

XML file parsing

Azkaban installation documentation
随机推荐
【Pygame 学习笔记】7.事件
Stable super odds, 9 years old mixin | 2022 Jincang innovative product launch was successfully held
Advanced C language -- custom type: structure enumeration Union
整理了一份通用的内存管理驱动代码
AcWing 257. Explanation of prisoner detention (bipartite picture)
Method of converting video format to ffmpeg and exporting GIF dynamic graph
PostgreSQL function usage record
FFmpeg转换视频格式与导出GIF动态图的方法
VIM strange unknown function 0
El table column drag and drop (no need to introduce other plug-ins)
sqli-labs(less-11)
Return to risk ratio: the most important indicator of investment opportunities 2020-03-14
LeetCode 0117. 填充每个节点的下一个右侧节点指针 II
【刷题记录】13. 罗马数字转整数
Ossimport migration path
Flask source code analysis (III): Context
In depth sorting: summary of machine learning modeling and parameter adjustment methods
MySQL advanced (VI) introduction to four common uses of fuzzy query
About the "bottom reading" mentality, it makes you exhausted 2020-03-15
Li Kou 413 division of equal difference sequence dynamic programming