public class Solution {
public int romanToInt(String s) {
int current = 0,prev,last;
HashMap<String, Integer> hm = new HashMap<>();
hm.put("I", 1);
hm.put("V", 5);
hm.put("X", 10);
hm.put("L", 50);
hm.put("C", 100);
hm.put("D", 500);
hm.put("M", 1000);
for(int i=s.length()-1; i>=0; i--){
last = hm.get(s.charAt(i)+"");
prev = i-1<0 ? 0 : hm.get(s.charAt(i-1)+"");
if(last > prev){
current += last-prev;
i--;
}else
current += last;
}
return current;
}
}
-----------------------------------------------
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
HINT:
Roman - I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)