-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_to_integer.cpp
More file actions
42 lines (41 loc) · 829 Bytes
/
Roman_to_integer.cpp
File metadata and controls
42 lines (41 loc) · 829 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
int romanCharToInt(char c)
{
switch(c)
{
case 'I':
case 'i':
return 1;
case 'V':
case 'v':
return 5;
case 'X':
case 'x':
return 10;
case 'L':
case 'l':
return 50;
case 'C':
case 'c':
return 100;
case 'D':
case 'd':
return 500;
case 'M':
case 'm':
return 1000;
default:
return 0;
}
}
int Solution::romanToInt(string A) {
int result = 0;
int n = A.length();
for (auto i = 0; i<n; ++i)
{
if (i!=n && romanCharToInt(A[i]) < romanCharToInt(A[i+1]))
result -= romanCharToInt(A[i]);
else
result += romanCharToInt(A[i]);
}
return result;
}