标签:
Roman to Integer
问题:
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) { int len = s.length() ; Map<Character, Integer> romanMap = new HashMap<Character,Integer>() ; romanMap.put(‘I‘,1) ; romanMap.put(‘V‘,5) ; romanMap.put(‘X‘,10) ; romanMap.put(‘L‘,50) ; romanMap.put(‘C‘,100) ; romanMap.put(‘D‘,500) ; romanMap.put(‘M‘,1000) ; int i = len - 1 ; int sum = romanMap.get(s.charAt(i)) ; i -- ; while(i >= 0) { int cur = romanMap.get(s.charAt(i)) ; int pre = romanMap.get(s.charAt(i + 1)) ; if( cur < pre) sum -= cur; else sum += cur ; i -- ; } return sum ; } }
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4315221.html