# Java Solution runs in 6 ms. What solution is faster?

• '''
public class Solution {
public int romanToInt(String s) {

int[] ascIIMap = new int[89];
ascIIMap['I'] = 1;
ascIIMap['V'] = 5;
ascIIMap['X'] = 10;
ascIIMap['L'] = 50;
ascIIMap['C'] = 100;
ascIIMap['D'] = 500;
ascIIMap['M'] = 1000;
int sum = 0;
int previousVal = 0;
for(int i=0;i<s.length();i++){

int currVal = ascIIMap[s.charAt(i)];

if(currVal<=previousVal ){
sum = sum + currVal;
}
else{

sum = sum - previousVal + (currVal-previousVal);

}
previousVal = currVal;
}
return sum;

}

}

'''
So this is the solution I came up with that runs in 6ms and I was wondering if anyone knows of a faster solution? There seems to be some that run in 5ms

• @nickwhat4

• use ``` backquotes instead of single quotes ''' to format code block
• your solution is good enough, perhaps you can try to use map to just retrieve instead of traversing but actually you should not pay too much attention to the time cost since there are some fluctuation in the OJ after all, as long as it's a good solution then it's okay.

• I did this and it ran in 5 ms:

int valCurr = 0;
int valPrev = 0;
int value = 0;

for (int i = s.length() - 1; i >= 0; i--) {
switch (s.charAt(i)) {
case 'i':
case 'I':
valCurr = 1;
break;
case 'v':
case 'V':
valCurr = 5;
break;
case 'x':
case 'X':
valCurr = 10;
break;
case 'l':
case 'L':
valCurr = 50;
break;
case 'c':
case 'C':
valCurr = 100;
break;
case 'd':
case 'D':
valCurr = 500;
break;
case 'm':
case 'M':
valCurr = 1000;
}
if (valCurr < valPrev) {
value = value - valCurr;
} else {
value = value + valCurr;
}
valPrev = valCurr;
}
return value;
```

• @rhajela Yes, there are sometimes when we have to balance between readability, tidiness and performance.

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