-
Notifications
You must be signed in to change notification settings - Fork 0
/
romantointeger.txt
25 lines (25 loc) · 1005 Bytes
/
romantointeger.txt
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
int romanToInt(string s) {
// Write your code here
long long int sum=0;
int i=0;
while(i<s.size()){
if(s[i]=='I'){
if(i!=s.size()-1 && s[i+1]=='V'){sum+=4;i=i+2;}
else if(i!=s.size()-1 && s[i+1]=='X'){sum=sum+9;i=i+2;}
else{sum=sum+1;i++;}
}else if(s[i]=='X'){
if(i!=s.size()-1 && s[i+1]=='L'){sum+=40;i=i+2;}
else if(i!=s.size()-1 && s[i+1]=='C'){sum=sum+90;i=i+2;}
else{sum=sum+10;i++;}
}else if(s[i]=='C'){
if(i!=s.size()-1 && s[i+1]=='D'){sum+=400;i=i+2;}
else if(i!=s.size()-1 && s[i+1]=='M'){sum=sum+900;i=i+2;}
else{sum=sum+100;i++;}
}else if(s[i]=='M'){sum=sum+1000;i++;}
else if(s[i]=='V'){sum=sum+5;i++;}
else if(s[i]=='L'){sum=sum+50;i++;}
else if(s[i]=='D'){sum=sum+500;i++;}
// cout<<sum<<endl;
}
return sum;
}