Roman to Integer
/**
* Given a roman numeral, convert it to an integer.
* Input is guaranteed to be within the range from 1 to 3999.
*/
public class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> dic = new HashMap<Character, Integer>();
dic.put('I', 1);
dic.put('V', 5);
dic.put('X', 10);
dic.put('L', 50);
dic.put('C', 100);
dic.put('D', 500);
dic.put('M', 1000);
//start from the rightmost one.
int res = dic.get(s.charAt(s.length() - 1));
for(int i = s.length() - 2; i >= 0; i--){
if(dic.get(s.charAt(i + 1)) <= dic.get(s.charAt(i)))
res += dic.get(s.charAt(i));
else
res -= dic.get(s.charAt(i));
}
return res;
}
}