Solution to LeetCode Roman to Integer problem.
This is quite easy. Just check if the current character has a smaller value than the next character. If so, use the subtractive rule, otherwise just adds its value and continue.
public int romanToInt(String s) {
HashMap<String,Integer> dict = new HashMap<String,Integer>();
dict.put("I",1);
dict.put("V",5);
dict.put("X",10);
dict.put("L",50);
dict.put("C",100);
dict.put("D",500);
dict.put("M",1000);
int sum = 0;
int i = 0;
while(i < s.length()){
String cur = s.substring(i,i+1);
if(i < s.length() - 1){
String next = s.substring(i+1,i+2);
if(dict.get(cur) < dict.get(next)){
sum += dict.get(next) - dict.get(cur);
i += 2;
continue;
}
}
sum += dict.get(cur);
i++;
}
return sum;
}