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) {
// Start typing your Java solution below
// DO NOT write main() function
if(s.length() <1) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
map.put('M', 1000);
map.put('D', 500);
map.put('C', 100);
map.put('L', 50);
map.put('X', 10);
map.put('V', 5);
map.put('I', 1);
int res = map.get(s.charAt(0));
int i=1;
while(i<s.length()){
int prev = map.get(s.charAt(i-1));
int cur = map.get(s.charAt(i));
res+=cur;
if(prev<cur)
res-=2*prev;
i++;
}
return res;
}
}