forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution013.cpp
More file actions
49 lines (44 loc) · 945 Bytes
/
solution013.cpp
File metadata and controls
49 lines (44 loc) · 945 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
49
/**
* @file Roman to integer.
*
* cpselvis ([email protected])
* August 8, 2016
*/
#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> umap;
umap.insert(make_pair('I', 1));
umap.insert(make_pair('V', 5));
umap.insert(make_pair('X', 10));
umap.insert(make_pair('L', 50));
umap.insert(make_pair('C', 100));
umap.insert(make_pair('D', 500));
umap.insert(make_pair('M', 1000));
int output = 0, last = 0, current;
for (int i = 0; i < s.length(); i ++)
{
if (umap.find(s[i]) != umap.end())
{
current = umap[s[i]];
output += current;
if (current > last)
{
output -= 2 * last;
}
last = current;
}
}
return output;
}
};
int main(int argc, char **argv)
{
Solution s;
cout << s.romanToInt("DCXXI") << endl;
cout << s.romanToInt("IMCLVI") << endl;
}