Skip to content

Commit 632519e

Browse files
Create 13. Roman to Integer.cpp
1 parent a0915ac commit 632519e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

13. Roman to Integer.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int romanToInt(string s) {
4+
unordered_map<char,int>mp =
5+
{{'I' , 1},{'V', 5},{'X' , 10},{'L' , 50},{'C' , 100},{'D', 500},{'M', 1000}};
6+
7+
int n = s.size(),total = 0;
8+
9+
for(int i = 0 ; i < n; i++){
10+
if( i + 1 < n && mp[s[i]] < mp[s[i+1]] ){
11+
total -= mp[s[i]];
12+
}
13+
else{
14+
total += mp[s[i]];
15+
}
16+
}
17+
return total;
18+
}
19+
};

0 commit comments

Comments
 (0)