当前位置:网站首页>[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;
}
}
}
边栏推荐
- 力扣今日题-522. 最长特殊序列
- Innovation and upgrading of supply chain system driven management mode in petrochemical industry and strengthening internal management of enterprises
- Fleet |「後臺探秘」第 3 期:狀態管理
- With 120billion yuan, she will ring the bell for IPO again
- Complete model training routine (I)
- 【算法篇】刷了两道大厂面试题,含泪 ”重学数组“
- R language ggplot2 visualization: the patchwork package is used to customize and combine the three ggplot2 visualization results to form a composite graph. After the horizontal combination of two sub
- 叮!Techo Day 腾讯技术开放日如约而至!
- Send2vec tutorial
- ROS knowledge points - definition and use of topic messages
猜你喜欢
SAP MTS/ATO/MTO/ETO专题之九:M+M模式前后台操作,策略用50,提前准备原材料和半成品
抽奖动画 - 鲤鱼跳龙门
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
[C language] how to generate normal or Gaussian random numbers
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
隆重推出 Qodana:您最爱的 CI 的代码质量平台
C语言学习-20-归并排序
How to build a 100000 level QPS large flow and high concurrency coupon system from zero
Fleet | background Discovery issue 3: Status Management
经典模型——Transformer
随机推荐
PostgreSQL 存储结构浅析
信创操作系统--麒麟Kylin桌面操作系统 (项目十 安全中心)
Send2vec tutorial
Express模板引擎
halcon 基础总结(一)裁切图片并旋转图像
笔试面试算法经典–最长回文子串
经典模型——Transformer
ORACLE中dbms_output.put_line输出问题的解决过程
Facebook出手!自适应梯度打败人工调参
Flutter dart语言特点总结
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
Ros21 lecture
openGauss内核:SQL解析过程分析
WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
Oracle11g数据库使用expdp每周进行数据备份并上传到备份服务器
ROS知识点——使用VScode搭建ROS开发环境
MIPS assembly language learning -02- logic judgment - foreground input
With a return of 5000 times, the South African newspaper invested in Tencent to make a province
What! One command to get the surveillance?
数组中的第K大元素[堆排 + 建堆的实际时间复杂度]