Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Summary: When meeting C/X/I, remembers to search forward to check if there is a bigger number at the front.
int romanToInt(string s) {
if(s.size() == )
return ; int num = ;
int m_idx = -;
int d_idx = -;
for(int i = ; i < s.size(); i ++) {
if(s[i] == 'C') {
m_idx = s.find_first_of('M', i + );
d_idx = s.find_first_of('D', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i)*;
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i)*;
i = d_idx;
}else {
num += ;
}
}else if( s[i] == 'M'){
num += ;
}else if(s[i] == 'D') {
num += ;
}else if(s[i] == 'L') {
num += ;
}else if(s[i] == 'X') {
m_idx = s.find_first_of('C', i + );
d_idx = s.find_first_of('L', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i)*;
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i)*;
i = d_idx;
}else {
num += ;
}
}else if(s[i] == 'V'){
num += ;
}else if(s[i] == 'I') {
m_idx = s.find_first_of('X', i + );
d_idx = s.find_first_of('V', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i);
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i);
i = d_idx;
}else {
num += ;
}
}
}
return num;
}