-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomantoInt.cpp
64 lines (63 loc) · 1.58 KB
/
RomantoInt.cpp
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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
class Solution {
public:
int romanToInt(std::string s) {
int solutionValue = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'I') {
if (i + 1 < s.length()) {
if (s[i + 1] == 'V') {
i++;
solutionValue += 4;
} else if (s[i + 1] == 'X') {
i++;
solutionValue += 9;
} else {
solutionValue += 1;
}
} else {
solutionValue += 1;
}
} else if (s[i] == 'X') {
if (i + 1 < s.length()) {
if (s[i + 1] == 'L') {
i++;
solutionValue += 40;
} else if (s[i + 1] == 'C') {
i++;
solutionValue += 90;
} else {
solutionValue += 10;
}
} else {
solutionValue += 10;
}
} else if (s[i] == 'C') {
if (i + 1 < s.length()) {
if (s[i + 1] == 'D') {
i++;
solutionValue += 400;
} else if (s[i + 1] == 'M') {
i++;
solutionValue += 900;
} else {
solutionValue += 100;
}
} else {
solutionValue += 100;
}
} else if (s[i] == 'V') {
solutionValue += 5;
} else if (s[i] == 'L') {
solutionValue += 50;
} else if (s[i] == 'D') {
solutionValue += 500;
} else if (s[i] == 'M') {
solutionValue += 1000;
}
std::cout << "Value of solutionValue: " << solutionValue << std::endl;
}
return solutionValue;
}
};