My simple Java Solution


  • 1
    X
    public class Solution {
    public int romanToInt(String s) {
            if (s.equals("")) {
                 return 0;
             } 
            Map<Character, Integer> m1 = new HashMap<Character, Integer>();
            m1.put('I',1);
            m1.put('V',5);
            m1.put('X',10);
            m1.put('L',50);
            m1.put('C',100);
            m1.put('D',500);
            m1.put('M',1000);
            int i=0;
            int result=0;
            char[] array=s.toCharArray();
            while (i<array.length) {
                if (i+1<array.length) {
                if (m1.get(array[i+1])>m1.get(array[i])) {
                        result=result+ m1.get(array[i+1])-m1.get(array[i]);
                        i++;
                        i++;
                        continue;
                        }   
                    }
                 result=result+m1.get(array[i]);
                 i++;
            }
            return result;
        }
    

    }


Log in to reply
 

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