-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.cpp
More file actions
47 lines (42 loc) · 856 Bytes
/
Solution.cpp
File metadata and controls
47 lines (42 loc) · 856 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
#include "../Test.h"
using namespace std;
using namespace leetcode;
class Solution {
public:
int romanToInt(string s) {
int sum = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s[i]) {
case 'I':
sum += (sum >= 5 ? -1 : 1);
break;
case 'V':
sum += 5;
break;
case 'X':
sum += (sum >= 50 ? -10 : 10);
break;
case 'L':
sum += 50;
break;
case 'C':
sum += (sum >= 500 ? -100 : 100);
break;
case 'D':
sum += 500;
break;
case 'M':
sum += 1000;
break;
}
}
return sum;
}
};
int main(int argc, char const *argv[]) {
Solution s;
test("case 1", s.romanToInt("III"), 3);
test("case 2", s.romanToInt("IV"), 4);
test("case 3", s.romanToInt("IX"), 9);
return 0;
}