本文共 639 字,大约阅读时间需要 2 分钟。
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Java代码:public class Solution {public int romanToInt(String s) { Mapmap = 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 i, total, pre, cur; total = map.get(s.charAt(0)); for (i = 1; i < s.length(); i++) { pre = map.get(s.charAt(i - 1)); cur = map.get(s.charAt(i)); if (cur <= pre) { total += cur; } else { total = total - pre * 2 + cur; } } return total; }}
转载地址:http://dnuni.baihongyu.com/