当前位置:网站首页>[leetcode] 13. Roman numeral to integer
[leetcode] 13. Roman numeral to integer
2022-06-28 15:27:00 【Xiaoqu】
13、 Roman numeral to integer
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1 .12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
- 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.
Given a Roman number , Convert it to an integer .
Example 1:
Input : s = "III"
Output : 3
Example 2:
Input : s = "IV"
Output : 4
Example 3:
Input : s = "IX"
Output : 9
Example 4:
Input : s = "LVIII"
Output : 58
explain : L = 50, V= 5, III = 3.
Example 5:
Input : s = "MCMXCIV"
Output : 1994
explain : M = 1000, CM = 900, XC = 90, IV = 4.
Tips :
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 .
Their thinking :
According to the description of the title , The rules can be summarized as follows :
- 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;
- It can be seen from the above that , The right value is always positive , So the last one must be positive .
blunt , Put a small value to the left of the large value , It's subtraction , Otherwise it's addition .
In code implementation , You can look back at one more , Compare the size relationship between the current bit and the next bit , So as to determine whether the current bit is added or subtracted . When there is no next , Just add .
You can also keep the value of the current bit , When traversing to the next bit , Compare the size relationship between the reserved value and the traversal bit , Then determine whether the retention value is plus or minus . The last one can add .
Staff code :
class Solution {
public int romanToInt(String s) {
int sum = 0;
int preNum = getValue(s.charAt(0));
for(int i = 1;i < s.length(); i ++) {
int num = getValue(s.charAt(i));
if(preNum < num) {
sum -= preNum;
} else {
sum += preNum;
}
preNum = num;
}
sum += preNum;
return sum;
}
private int getValue(char ch) {
switch(ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
}

边栏推荐
- 叮!Techo Day 腾讯技术开放日如约而至!
- R语言ggplot2可视化:使用patchwork包将两个ggplot2可视化结果横向构成新的结果可视化组合图(使用|符号)
- 一个bug肝一周...忍不住提了issue
- 【算法篇】刷了两道大厂面试题,含泪 ”重学数组“
- ROS knowledge points - ROS create workspace
- ROS知识点——ROS创建工作空间
- 力扣今日题-522. 最长特殊序列
- DBMS in Oracle_ output. put_ Line output problem solving process
- Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)
- Analysis of PostgreSQL storage structure
猜你喜欢

The hidden crisis of Weilai: past, present and future

Cross cluster deployment of helm applications using karmada

厨卫电器行业S2B2C系统网站解决方案:打造S2B2C平台全渠道商业系统

C#/VB. Net to convert PDF to excel
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem

C#/VB.NET 将PDF转为Excel

Privacy computing fat - offline prediction

GCC efficient graph revolution for joint node representationlearning and clustering

第四大运营商,难成「鲶鱼」

Talking about open source - Linus and Jim talk about open source in China
随机推荐
Ros21 lecture
Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]
开源大咖说 - Linus 与 Jim 对话中国开源
[C language] how to implement plural types
信创操作系统--麒麟Kylin桌面操作系统 (项目十 安全中心)
The past and present life of distributed cap theorem
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合起来形成单个可视化结果图
Cross cluster deployment of helm applications using karmada
R语言ggplot2可视化:使用patchwork包将3个ggplot2可视化结果自定义组合起来构成组合图、两个子图横向组合后和另外一个图纵向组合构成最终组合图
With 120billion yuan, she will ring the bell for IPO again
ROS21讲
笔试面试算法经典–最长回文子串
Web worker poll request
How to solve the following problems in the Seata database?
C#/VB.NET 将PDF转为Excel
经典模型——Transformer
[C language] how to generate normal or Gaussian random numbers
ROS knowledge points - definition and use of topic messages
sql语句 练习题
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a data table to form a final result graph