-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#13.cc
35 lines (34 loc) · 1.05 KB
/
LeetCode#13.cc
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
class Solution {
public:
int romanToInt(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
if(len<=0) return 0;
int ret = 0;
int i =0;
while(i<len){
if(s[i]=='M') ret+=1000;
else if(s[i]=='D') ret+=500;
else if(s[i]=='C'){
if(i!=len-1&&s[i+1]=='M') {ret+=900;i++;}
else if(i!=len-1&&s[i+1]=='D'){ret+=400;i++;}
else ret+=100;
}
else if(s[i]=='L') ret+=50;
else if(s[i]=='X'){
if(i!=len-1&&s[i+1]=='L'){ret+=40;i++;}
else if(i!=len-1&&s[i+1]=='C'){ret+=90;i++;}
else ret+=10;
}
else if(s[i]=='V') ret+=5;
else if(s[i]=='I'){
if(i!=len-1&&s[i+1]=='V'){ret+=4;i++;}
else if(i!=len-1&&s[i+1]=='X'){ret+=9;i++;}
else ret+=1;
}
i++;
}
return ret;
}
};