-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroman_numbers1.cpp
More file actions
72 lines (69 loc) · 1.84 KB
/
roman_numbers1.cpp
File metadata and controls
72 lines (69 loc) · 1.84 KB
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
65
66
67
68
69
70
71
// Solution for
// https://leetcode.com/problems/roman-to-integer/description/
class Solution {
public:
int romanToInt(string s) {
int temp=0;
for(int i=0; i<s.size(); ++i)
{
switch(s[i])
{
case 'M':
temp += 1000;
break;
case 'D':
temp += 500;
break;
case 'C':
if(s[i+1]=='M')
{
temp += 900;
i++;
}
else
if(s[i+1]=='D')
{
temp += 400;
i++;
}
else temp += 100;
break;
case 'L':
temp += 50;
break;
case 'X':
if(s[i+1]=='C')
{
temp += 90;
i++;
}
else
if(s[i+1]=='L')
{
temp += 40;
i++;
}
else temp += 10;
break;
case 'V':
temp += 5;
break;
case 'I':
if(s[i+1]=='X')
{
temp += 9;
i++;
}
else
if(s[i+1]=='V')
{
temp += 4;
i++;
}
else temp += 1;
break;
}
}
return temp;
}
};