标签:
public int romanToInt(String s) { //数字:Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) HashMap<Character, Integer> map = new HashMap<>(); 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 = map.get(s.charAt(s.length() - 1)); int result = cur; for (int i = s.length() - 2; i >= 0; --i) { int pre = map.get(s.charAt(i)); if (cur > pre) { result -= pre; } else { result += pre; } cur = pre; } return result; }
标签:
原文地址:http://www.cnblogs.com/fripside/p/4612168.html