Input is guaranteed to be within the range from 1 to 3999.
这个和之前的相比就是倒序一下
public int romanToInt(String s) { int result = 0; if(s != null && s.length()>0){ /* String[] ge = {"I", "II","III", "IV","V","VI","VII", "VIII","IX"}; String[] hen = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; String[] he = {"C","CC","CCC","CD", "D","DC","DCC","DCCC","CM"}; String[] thr = {"M","MM","MMM"};*/ HashMap<Character,Integer> hm = new HashMap<Character,Integer>(); hm.put('I', 1); hm.put('V', 5); hm.put('X', 10); hm.put('L', 50); hm.put('C', 100); hm.put('D', 500); hm.put('M', 1000); int len = s.length(); for(int i = 0 ;i<len;i++){ int temp = hm.get(s.charAt(0)); if(s.charAt(0)== 'I'){ temp = (s.contains("V")||s.contains("X"))?-temp:temp; }else if(s.charAt(0) == 'X'){ temp = (s.contains("L")||s.contains("C"))?-temp:temp; }else if(s.charAt(0) == 'C'){ temp = (s.contains("D")||s.contains("M"))?-temp:temp; } s = s.substring(1); result += temp; } } return result; }