1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | public int romanToInt(String s) {         int ans=0;
          Map<String,Integer> map=new HashMap<>();         map.put("M",1000);         map.put("CM",900);         map.put("D",500);         map.put("CD",400);         map.put("C",100);         map.put("XC",90);         map.put("L",50);         map.put("XL",40);         map.put("X",10);         map.put("IX",9);         map.put("V",5);         map.put("IV",4);         map.put("I",1);
          for (int i=0;i<s.length(); ){
              if (i+1<s.length()&&map.containsKey(s.substring(i,i+2))){                 ans+=map.get(s.substring(i,i+2));                 i+=2;             }else {                 ans+=map.get(s.substring(i,i+1));                 i++;             }         }         return ans;     }
  |