關於LeetCode中Roman to Integer一題的理解
阿新 • • 發佈:2019-01-29
題目如下:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
public int romanToInt(String s) { HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("I",1); map.put("V",5); map.put("X",10); map.put("C",100); map.put("M",1000); map.put("L",50); map.put("D",500); int result = 0; for(int i=0;i<s.length();i++){ int current = map.get(s.charAt(i)+"").intValue(); if(i<s.length()-1){ int next = map.get(s.charAt(i+1)+"").intValue(); result += next>current?current*(-1):current; }else{ result += current; } } return result; }