# Java clean and fast solution

• ``````    public int romanToInt(String s) {
int num = 0;
int l = s.length();
int last = 1000;
for (int i = 0; i < l; i++){
int v = getValue(s.charAt(i));
if (v > last) num = num - last * 2;
num = num + v;
last = v;
}
return num;
}
private int getValue(char c){
switch(c){
case 'I' : return 1;
case 'V' : return 5;
case 'X' : return 10;
case 'L' : return 50;
case 'C' : return 100;
case 'D' : return 500;
case 'M' : return 1000;
default : return 0;
}
}``````

• This post is deleted!

• i don't understand. pls give me some clear suggestion.eg:

``````if (v > last) num = num - last * 2;
num = num + v;
last = v;``````

• for example , IX, when i=1, num = 1, last = 1, now I.value < X.value, so num should minus the last first ,then plus the value (X.value-I,value) secondly. So it is num = num - last*2 +v.

• The input sequence should be displayed in descending order unless encounting violation numbers. Then you should think about what numbers may cause a violation. You can find out that the numbers like "IV", "IX" will cause a violation. At these cases, the last number "I" has been added to the final result at last phase but actually it should be diducted from current number"X", therefore at current phase, it should be deducted twice. We get this value 9 by this equation 1 + 10 - 2 = 9

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