当前位置:网站首页>Leetcode-13- Roman numeral to integer (simple)
Leetcode-13- Roman numeral to integer (simple)
2022-06-13 00:59:00 【Didi dada】
13. Roman numeral to integer ( Simple )
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 :
ICan be placed inV(5) andX(10) Left side , To express 4 and 9.XCan be placed inL(50) andC(100) Left side , To express 40 and 90.CCan be placed inD(500) andM(1000) Left side , To express 400 and 900.
Given a Roman number , Convert it to an integer . Input to ensure 1 To 3999 Within the scope of .
Ideas :
- When the string does not exist ’IV\IX\XL\XC\CD\CM’ when , Each character of the string represents a number , And , here , The number represented by the preceding character is not less than the number represented by the following character , Such as
LVIII=50+5+1+1+1=58. - When... Exists in the string ’IV\IX\XL\XC\CD\CM’ when , As such IV The value represented is V-I=4. therefore , When the number represented by the previous character is less than the next character when , The number represented by the previous character needs to be subtracted .
- therefore , Traverse Roman numerals , Judge the relationship between the current character and the next character , If the current character is smaller then res Subtract this number , otherwise , Add this number .
(1)C++
class Solution {
public:
int romanToInt(string s) {
char a[] = {'M','D','C','L','X','V','I'};
int b[] = { 1000,500,100,50,10,5,1 };
int n = s.length();
int res = 0;
map<char , int> m;
for(int i=0; i<7; i++){
m[a[i]]=b[i];
}
for(int j=0;j<n-1;j++){ //C++ No boundary detection in , You need to pay attention to yourself
if(m[s[j]]<m[s[j+1]])
res-=m[s[j]];
else
res+=m[s[j]];
}
res+=m[s[n-1]];
return res;
}
};
(2)python
class Solution:
def romanToInt(self, s: str) -> int:
a = {
"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}
temp = 0
res = 0
for each in s:
res += a[each]
if(a[each]>temp):
res -= 2*temp
temp = a[each]
return res
边栏推荐
- Undirected graph -- computing the degree of a node in compressed storage
- [latex] insert picture
- Biological unlocking - Fingerprint entry process
- 【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
- kotlin 协程withContext切换线程
- redis
- Go simple read database
- Binary tree -- using hierarchical sequence and middle sequence to determine a tree
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
- Cards are unpredictable
猜你喜欢

市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?

Liu Hui and introduction to nine chapter arithmetic and island arithmetic

MySQL index

Five classic articles worth reading

Kotlin 协程的四种启动模式

AOF持久化
![[JS component] previous queue prompt](/img/79/9839f68b191b0db490e9bccbeaae1d.jpg)
[JS component] previous queue prompt

Pipeline pipeline project construction

Antdpro - protable realizes the linkage effect of two selection boxes

Development notes of Mongoose
随机推荐
[JS component library] drag sorting component
MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
Rotating camera
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
Sequence table - find main element
Opencv face recognition of ros2 foxy~galactic~humble
.net core 抛异常对性能影响的求证之路
ROS从入门到精通(零) 教程导读
[JS component] previous queue prompt
刘徽与《九章算术》《海岛算经》简介
(01). Net Maui actual construction project
软件测试的几种分类,一看就明了
Androi weather
Minimum spanning tree problem
Hard (magnetic) disk (II)
Three threads print digital demo alternately
Quick power explanation
市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?
[JS component] simulation framework
The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro