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;
}
}
Java clean and fast solution


i don't understand. pls give me some clear suggestion.eg:
if (v > last) num = num  last * 2; num = num + v; last = 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