class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
symbol={"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
total=0
s=s[::-1]
last=None
for x in s:
if last>symbol[x] and last:
total-=symbol[x]
else:
total+=symbol[x]
last=symbol[x]
return total

public int romanToInt(String s) {
HashMap<Character,Integer> romanMap = new HashMap<Character,Integer>();
romanMap.put('I',1);
romanMap.put('V',5);
romanMap.put('X',10);
romanMap.put('L',50);
romanMap.put('C',100);
romanMap.put('D',500);
romanMap.put('M',1000);
char[] input = s.toCharArray();
int sum = 0;
for(int i=0; i<input.length; i++){
int left = romanMap.get(input[i]);
int right = 0;
if ((i+1) < input.length){
right = romanMap.get(input[i+1]);
}
if(left >= right){
sum = sum + left;
}else{
sum = sum - left;
}
}
return sum;

@edd91 I have changed the post a bit, to make it more clear. I hope your doubt is now solved. Dont think it as a problem of detection of numbers, but to evaluate the final result.