forked from Viv786ek/Leetcode-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman to interger.cpp
More file actions
44 lines (35 loc) · 1.19 KB
/
Roman to interger.cpp
File metadata and controls
44 lines (35 loc) · 1.19 KB
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
40
41
42
43
44
int romanToInt(string s) {
unordered_map<char, int> mp = {{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}};
int res = mp[s.back()];
for(int i = 0; i < s.size() - 1; i++) {
if(mp[s[i]] < mp[s[i + 1]]) res -= mp[s[i]];
else res += mp[s[i]];
}
return res;
}
class Solution {
public:
int romanToInt(string s) {
int ret = 0; // to store the return value
int temp = 0; // to store the previous value
for (size_t i = 0; i < s.size(); i++) {
char curr = s[i];
int pos = 0; // to store the current value
switch(curr) {
case 'I': pos = 1; break;
case 'V': pos = 5; break;
case 'X': pos = 10; break;
case 'L': pos = 50; break;
case 'C': pos = 100; break;
case 'D': pos = 500; break;
case 'M': pos = 1000; break;
default: return 0;
}
ret += pos;
if (temp < pos)
ret -= temp*2; // ex: IV, ret = 1 + 5 - 1*2 = 4
temp = pos;
}
return ret;
}
};