-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_roman_integer.cpp
More file actions
27 lines (25 loc) · 846 Bytes
/
13_roman_integer.cpp
File metadata and controls
27 lines (25 loc) · 846 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
#include <vector>
#include <string>
#include <unordered_map>
#include <iterator>
#include <algorithm>
class Solution {
public:
int romanToInt(std::string s) {
std::unordered_map<std::string, int> mp = {{"I", 1}, {"V", 5}, {"X", 10}, {"L", 50},{"C", 100},{"D", 500},{"M", 1000}, {"IV", 4}, {"IX", 9}, {"XL", 40}, {"XC", 90}, {"CD", 400}, {"CM", 900}};
std::vector<std::string> vec = {"IV", "IX", "XL", "XC", "CD", "CM"};
int num = 0;
for(int i = 0; i < vec.size(); i++){
auto find = s.find(vec[i]);
if(find != std::string::npos) {
num += mp[vec[i]];
s.erase(find, 2);
}
}
for(int i = 0; i < s.size(); i++){
std::string temp(1, s[i]);
num += mp[temp];
}
return num;
}
};