-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanToInt.cpp
More file actions
48 lines (40 loc) · 897 Bytes
/
Copy pathromanToInt.cpp
File metadata and controls
48 lines (40 loc) · 897 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <unordered_map>
#include <cstdint>
#include <iostream>
#include <string>
class Solution {
public:
int romanToInt(std::string s) {
int result = 0;
std::unordered_map<char, std::int16_t> romain = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
std::int16_t prev = 0;
for(auto rit = s.rbegin(); rit != s.rend(); ++rit)
{
auto value = romain[*rit];
if(value >= prev)
{
result += value;
}
else
{
result -= value;
}
prev = value;
}
return result;
}
};
int main()
{
Solution s;
std::cout << s.romanToInt("III") << "\n"; // 1994
return 0;
}