当前位置:网站首页>[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
边栏推荐
- Uio-66 - (COOH) 2 modified polyamide nanofiltration membrane | zif-8/pvp composite nanofiber membrane | uio-66-nh2 modified polyamide nanofiltration membrane
- 2022年最新吉林建筑安全员模拟题库及答案
- Label ball problem
- [try to hack] ARP and ARP deception
- 【Try to Hack】arp和arp欺骗
- Force buckle 64 minimum path sum -- Introduction to dynamic programming
- 如何在MFC中添加一个线程
- Which domestic API tool is better? It turned out to be it
- AE如何制作星云粒子特效
- [pyGame learning notes] 6 Cursor mouse cursor
猜你喜欢

R language -- principle of Cox model calibration curve (I) data source

音频常见端子剖析图---再也不会搞错了

Differences between get requests and post requests and usage examples
![[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

Force buckle 64 minimum path sum -- Introduction to dynamic programming

MatrixCube揭秘 101——MatrixCube的功能与架构

Using the case statement will produce a latch condition

力扣64-最小路径和——动态规划入门题型

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

Security measures for tcp/ip protocol vulnerabilities
随机推荐
Will the market halve? What investment opportunities are there? 2020-03-11
R language -- principle of Cox model calibration curve (I) data source
How to invest scientifically and rationally when the global financial crisis strikes? 2020-03-17
Ultrasonic sensor (ch101 & ch201) - Ⅱ
A general memory management driver code is sorted out
Ossimport migration path
如何在MFC中添加一个线程
松下A6伺服驱动器外部绝对值光栅尺全闭环参数设置
Lanthanide metal organic skeleton( [email protected] )|Rhodamine 6G modified MOF material | catalase @zif composite | MOF
Minimum exchange times
稳超胜算,历9弥新 | 2022金仓创新产品发布会顺利召开
Return to risk ratio: the most important indicator of investment opportunities 2020-03-14
Azkaban installation documentation
XML建模(简单易学)
Li Kou 70 - climbing stairs - Dynamic Planning
506. Relative ranking
Label ball problem
国产API工具哪家强?原来是它…
ASP. Net collaborative OA office service management platform source code
Ultrasonic sensor (ch101 & ch201) - I