-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman.cpp
More file actions
32 lines (27 loc) · 712 Bytes
/
Roman.cpp
File metadata and controls
32 lines (27 loc) · 712 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
class Solution {
public:
int romanToInt(string s) {
map <char,int> val;
val['I']=1;
val['V']=5;
val['X']=10;
val['L']=50;
val['C']=100;
val['D']=500;
val['M']=1000;
int ans{};
int temp{};
for(int i = 0 ; i< s.length(); ++i)
{
if(val[s[i]] < val[s[i+1]] && i+1 < s.length()) //compare adj no
{
temp = val[s[i+1]] - val[s[i]];
i++; // not to compare ith index twice
}
else
temp = val[s[i]];
ans += temp;
}
return ans;
}
};