-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanToInteger.cpp
54 lines (52 loc) Β· 986 Bytes
/
RomanToInteger.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
https://leetcode.com/problems/roman-to-integer/submissions/1211846513/
class Solution {
public:
int romanToInt(string s) {
int sum = 0 , index = 0;
while(index<s.size()-1)
{
if(num(s[index])<num(s[index+1]))
{
sum-=num(s[index]);
}
else
{
sum+=num(s[index]);
}
index++;
}
sum+=num(s[s.size()-1]);
return sum;
}
int num(char c)
{
if(c == 'I')
{
return 1;
}
else if(c == 'V')
{
return 5;
}
else if(c == 'X')
{
return 10;
}
else if(c == 'L')
{
return 50;
}
else if(c == 'C')
{
return 100;
}
else if(c == 'D')
{
return 500;
}
else
{
return 1000;
}
}
};