当前位置:网站首页>[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;
}
}
}

边栏推荐
- 浪擎与浪潮,一个从OEM到价值共生的生态样板
- 使用Karmada实现Helm应用的跨集群部署
- WSUS客户端访问服务端异常报错-0x8024401f「建议收藏」
- Realization of a springboard machine
- PostgreSQL 存储结构浅析
- Solution to JSON parsing problem using curl for Tron API signature broadcast and json Problem record of the loads method
- 蔚来潜藏的危机:过去、现在到未来
- MySQL主从切换的超详细步骤
- 第四大运营商,难成「鲶鱼」
- 浪潮网络步步为赢
猜你喜欢

经典模型——Transformer
ORACLE中dbms_output.put_line输出问题的解决过程

Analysis of PostgreSQL storage structure

Facebook! Adaptive gradient defeats manual parameter adjustment

MIPS assembly language learning -02- logic judgment - foreground input

Fleet | "backstage exploration" issue 3: status management

Fleet |「后台探秘」第 3 期:状态管理
Oracle11g数据库使用expdp每周进行数据备份并上传到备份服务器

Fleet |「後臺探秘」第 3 期:狀態管理

Fleet | background Discovery issue 3: Status Management
随机推荐
笔试面试算法经典–最长回文子串
Experiment 6 8255 parallel interface experiment [microcomputer principle] [experiment]
Send2vec tutorial
【LeetCode】13、罗马数字转整数
力扣今日题-522. 最长特殊序列
华为能成“口红一哥”,或者“带货女王”吗?
Leetcode 48. Rotate image (yes, resolved)
化学制品制造业智慧供应商管理系统深度挖掘供应商管理领域,提升供应链协同
Classmate Zhang hasn't learned to be an anchor yet
C语言基础语法
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine the two ggplot2 visualization results to form a single visualization result graph
币圈大地震:去年赚100万,今年亏500万
Grand launch of qodana: your favorite CI code quality platform
Cross cluster deployment of helm applications using karmada
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一段文本内容横向组合起来形成最终结果图
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
Halcon basic summary (I) cutting pictures and rotating images
Analysis of PostgreSQL storage structure
C语言学习-19-全排列
[C language] how to implement plural types