-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0001.cpp
More file actions
30 lines (30 loc) · 750 Bytes
/
DAY0001.cpp
File metadata and controls
30 lines (30 loc) · 750 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
// 13. Roman to Integer
class Solution {
public:
int checkvalue(char c){
int x=0;
if(c=='I') x= 1;
if(c=='V') x= 5;
if(c=='X') x= 10;
if(c=='L') x= 50;
if(c=='C') x= 100;
if(c=='D') x= 500;
if(c=='M') x= 1000;
return x;
}
int romanToInt(string s) {
int value=0,x,y=checkvalue(s[0]);
for(int i=1;i<s.length();i++){
x=checkvalue(s[i]);
if(y>=x){
value+=y;
}
else{
value-=y;
}
y=x;
}
value+=y;
return value;
}
};