My straightforward Java solution using hashmap


  • 7
    N

    I am quite strange to Roman numeric, I came up with this solution after I referred to Wikipedia. This solution is quite straightforward. Run-time: 452 ms

    public int romanToInt(String s) {
        HashMap<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 len = s.length();
        int index = 0, result = 0;
        while (index < len) {
            Character chCur = s.charAt(index);
            Character chNext = null;
            if (index + 1 < len)
                chNext = s.charAt(index + 1);
            if(chNext != null && map.get(chCur) < map.get(chNext))
                result -= map.get(chCur);
            else
                result += map.get(chCur);
            index++;
        }
        return result;
    }

  • 0
    M

    Can this solution guarantee the output is between 1 and 3999?


  • 0
    N

    If I remember it correctly, the OJ test cases test it all from 1 to 3999, so yes.


  • 0
    R

    Made it more simple and little bit faster

    public class Solution {
        public int romanToInt(String s) {
            int res = 0;
            HashMap<Character,Integer> map = new HashMap<Character,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);
            res += map.get(s.charAt(0));
            for(int i=1;i<s.length();i++){
                int cur = map.get(s.charAt(i));
                int pre = map.get(s.charAt(i-1));
                if (pre<cur) res -= pre*2;
                res += cur;
            }
            return res;
        }
    }
    

  • 0
    P

    @NathanLvzs it failed "MMDLV" due to TLE


  • 0
    U

    No need to put so many checks

    public class Solution {
        public int romanToInt(String s) {
            Map<Character, Integer> map = new HashMap<Character, 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);
            int val=0;
            int preIndex = 0;
            
            for(int i=s.length()-1;i>=0;i--){
                if(map.get(s.charAt(i))<preIndex){
                    val = val - map.get(s.charAt(i));
                }else{
                    val = val + map.get(s.charAt(i));
                }
                preIndex = map.get(s.charAt(i));
            }
            
            return val;
            
        }
    }
    

  • 0
    K

    @NathanLvzs Can you please tell me why mine is not working for MCMXCVI ?
    class Solution {
    public int romanToInt(String s) {
    Map <Character,Integer> map=new HashMap<Character,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);
    Character c;
    int l = s.length();
    int val,pval=1000;
    int n=0;
    for(int i=0;i<l;i++)
    {
    c=s.charAt(i);
    val=map.get(c);
    if(val<=pval)
    n+=val;
    else
    n-=val;
    pval=val;

        }
        return n;
    }
    

    }


  • 1
    B

    @Kacys

    The Format of your code is little misleading, but if I understand it correct:

    else{
     n-=val;
    }
    

    is actually wrong. Just check for "IX". You might wanna do:

    n+=(val-2pval);
    

    Here's my complete code, similar to your idea with corrections.
    In addition, HashMap<String,Integer> seems to perform better than HashMap<Character,Integer>:

    class Solution {
        public int romanToInt(String s) {
            HashMap <String,Integer> roman = new HashMap<String,Integer>();
            roman.put("I",1);
            roman.put("V",5);
            roman.put("X",10);
            roman.put("L",50);
            roman.put("C",100);
            roman.put("D",500);
            roman.put("M",1000);
            
            int cur=0,prev=Integer.MAX_VALUE,res=0;
            for(int i=0;i<s.length();i++){
                cur=roman.get(String.valueOf(s.charAt(i)));
                if(prev<cur){
                    res-=prev;
                    res+=(cur-prev);
                } else{
                    res+=cur;
                }
                prev=cur;
            }
            
            return res;
        }
    }
    
    

  • 0
    K

    @bpk1993 Thank you


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.