把一个罗马字符串转换成数字
public class Solution {
static HashMap<Character, Integer> map = new HashMap<>(47);
public Solution(){
this.put('I', 1).put('V', 5).put('X', 10).put('L', 50).put('C', 100)
.put('D', 500).put('M', 1000);
}
private Solution put(Character c, Integer i){
map.put(c, i);
return this;
}
public int romanToInt(String s) {
int ans = 0;
char chars[] = s.toCharArray();
for(int i = 0; i < chars.length; ++i){
Integer pre = map.get(chars[i]);
Integer next = null;
if(i + 1 < chars.length){
next = map.get(chars[i + 1]);
}
if(next != null && pre < next){
ans += next - pre;
++i;
}
else{
ans += pre;
}
}
return ans;
}
}