-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb1.cpp
More file actions
55 lines (48 loc) · 1.12 KB
/
fb1.cpp
File metadata and controls
55 lines (48 loc) · 1.12 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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
unordered_map<string, int> populateMap() {
unordered_map<string, int> umap;
umap["I"] = 1;
umap["V"] = 5;
umap["IV"] = 4;
umap["X"] = 10;
umap["IX"] = 9;
umap["L"] = 50;
umap["XL"] = 40;
umap["C"] = 100;
umap["XC"] = 90;
umap["D"] = 500;
umap["CD"] = 400;
umap["M"] = 1000;
umap["CM"] = 900;
return umap;
}
int romanToInt(string s) {
unordered_map <string, int> umap = populateMap();
string::iterator it = s.begin();
int res = 0;
while(it != s.end()){
string singleChar = "";
singleChar.push_back(*it);
string doubleChar = singleChar;
doubleChar.push_back(*(it + 1));
if(umap[doubleChar]) {
res += umap[doubleChar];
++it;
} else {
res += umap[singleChar];
}
++it;
}
return res;
}
};
int main() {
Solution s;
int res = s.romanToInt("MCMXCIV");
cout << res << endl;
return 0;
}