-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman to Integer.cpp
More file actions
39 lines (35 loc) · 798 Bytes
/
Copy pathRoman to Integer.cpp
File metadata and controls
39 lines (35 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* problem link :https://leetcode.com/problems/roman-to-integer/
* problem name: 13. Roman to Integer
* Status: Accepted.
* Author : Mohand sakr.
*/
class Solution {
public:
int romanToInt(string s) {
map <char,int> ma;
ma['I']=1;
ma['V']=5;
ma['X']=10;
ma['L']=50;
ma['C']=100;
ma['D']=500;
ma['M']=1000;
int len=s.length();
int sum=0;
int past=0;
for(int i=0;i<len;i++){
int current=ma[s[i]];
if(current<=past){
sum+=current;
past=current;
}
else {
sum-=past;
sum+=(current-past);
past=current;
}
}
return sum;
}
};