mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
18 lines
550 B
C++
18 lines
550 B
C++
class Solution{
|
|
public:
|
|
int romanToInt(string s){
|
|
unordered_map<char,int> convert = {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
|
|
int result = 0;
|
|
for(int i=0;i<s.size();++i){
|
|
int curr = convert[s[i]];
|
|
int next = ((i+1) < s.size())? convert[s[i+1]] : 0;
|
|
if(curr<next){
|
|
result-=curr;
|
|
}else{
|
|
result+=curr;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
};
|