13. Roman to Integer

来源:互联网 发布:淘宝北京新大陆可靠吗 编辑:程序博客网 时间:2024/06/11 21:57

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 res = 0;//char[] str = s.toCharArray();String[] strs = new String[s.length()];for(int i=0;i<s.length();i++){    strs[i] = s.substring(i,i+1);}HashMap<String,Integer> map = new HashMap<String,Integer>();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);String max = "I";for(int i=s.length()-1;i>=0;--i){if(map.get(strs[i])>=map.get(max)){max = strs[i];res += map.get(strs[i]);}else{res -= map.get(strs[i]);}}        return res;    }}


0 0
原创粉丝点击