-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion13.cpp
More file actions
32 lines (32 loc) · 836 Bytes
/
question13.cpp
File metadata and controls
32 lines (32 loc) · 836 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
class Solution {
public:
int romanToInt(string s) {
int arr[]={'I','V','X','L','C','D','M'};
int val[]={1,5,10,50,100,500,1000};
int sum=0;
int i,j;
for(i=0;i<s.length()-1;i++)
{
char ch=s.at(i);
char ch2=s.at(i+1);
for(j=0;j<7;j++)
{
if(arr[j]==ch)
{
if((ch=='C'&&(ch2=='D'||ch2=='M'))||(ch=='I'&&(ch2=='V'||ch2=='X'))||(ch=='X'&&(ch2=='L'||ch2=='C')))
sum-=val[j];
else
sum+=val[j];
}
}
}
for(j=0;j<7;j++)
{
if(arr[j]==s.at(s.length()-1))
{
sum+=val[j];
}
}
return sum;
}
};