Roman to Integer

来源:互联网 发布:淘宝模特室内怎么布光 编辑:程序博客网 时间:2024/05/20 01:39
public class Solution {    public int romanToInt(String s) {        if (s == null || s.length() == 0) {            return 0;        }        Map<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 result = map.get(s.charAt(s.length() - 1));        for (int i = s.length() - 2; i >= 0; i--) {            if (map.get(s.charAt(i)) >= map.get(s.charAt(i + 1))) {                result = result + map.get(s.charAt(i));            } else {                result = result - map.get(s.charAt(i));            }        }        return result;    }}

0 0
原创粉丝点击