-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roman to Integer.cpp
60 lines (55 loc) · 1.18 KB
/
Roman to Integer.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
class Solution {
public:
int romanToInt(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int tmp = 0;
int input = 0, next = 0;
int i = 0;
int size = s.size();
for (i = 0; i < size - 1; i++)
{
input = getValue(s[i]);
next = getValue(s[i + 1]);
if (input < next)
{
tmp -= input;
}
else
{
tmp += input;
}
}
if (size >= 1)
tmp += getValue(s[size - 1]);
return tmp;
}
int getValue(char c)
{
switch(c)
{
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
return 0;
}
};