-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_Numerals_Decoder.cpp
More file actions
55 lines (38 loc) · 960 Bytes
/
Copy pathRoman_Numerals_Decoder.cpp
File metadata and controls
55 lines (38 loc) · 960 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
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int solution(string roman) {
// Your code here
unordered_map<char, int> romans{ {'M',1000}, {'D', 500},{'C', 100},{'L', 50}, {'X', 10}, {'V', 5}, {'I', 1} };
int sum = 0;
int prev = 0;
reverse(roman.begin(), roman.end());
for (auto i : roman)
{
int val = romans[i];
sum += (val < prev) ? -val : val;
prev = val;
}
return sum;
}
/* BEST CODE
#include <iostream>
#include <string>
using namespace std;
map<char, int> nums = {{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}};
int solution(string roman) {
int res = 0;
int old = 0;
for(auto a : roman){
int curr = nums[a];
res += curr;
if(curr > old){
res -= 2 * old;
}
old = curr;
}
return res;
}
*/