Easy to understand Fast running Java Solution


#1

public class Solution {
public int romanToInt(String A) {
String[] roman = new String[]{“M”,“CM”,“D”,“CD”,“C”,“XC”,“L”,“XL”,“X”,“IX”,“V”,“IV”,“I”};
int[] val = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
int res = 0;

    for(int i=0; i<roman.length; i++){
        while(A.length()>0 && A.startsWith(roman[i])){
            res+=val[i];
            A = A.substring(roman[i].length());
        }
    }
    return res;
}

}