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) {
s = s.trim();
if (null == s || "".equals(s))
return 0;
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
int cur = s.length()-1;
int sum = map.get(s.charAt(cur));
int tmp1,tmp2;
cur--;
while(cur>=0){
tmp1 = map.get(s.charAt(cur+1));
tmp2 = map.get(s.charAt(cur));
if(tmp2<tmp1)
sum = sum - tmp2;
else
sum = sum + tmp2;
cur--;
}
return sum;
} public static void main(String[] args){
String param = "CMXLVI";
if(args.length==1){
param = args[0];
}
Solution solution = new Solution();
int res = solution.romanToInt(param);
System.out.println(res);
}
}