@makeittrue said in My solution for this question but I don't know is there any easier way?:

My solution, only scan string once

public static int romanToInt(String s) { int res = 0; for (int i = s.length() - 1; i >= 0; i--) { char c = s.charAt(i); switch (c) { case 'I': res += (res >= 5 ? -1 : 1); break; case 'V': res += 5; break; case 'X': res += 10 * (res >= 50 ? -1 : 1); break; case 'L': res += 50; break; case 'C': res += 100 * (res >= 500 ? -1 : 1); break; case 'D': res += 500; break; case 'M': res += 1000; break; } } return res; }

This is a very clean and elegant solution mainly because it directly follow the roman numeral rules.

I had a trouble understanding when the subtraction happen in some of the cases.

This link was helpful to understand it: https://www.knowtheromans.co.uk/Categories/SubCatagories/RomanNumerals/

There are six instances where subtraction is used: ⋅ I can be placed before V (5) and X (10) to make 4 and 9. ⋅ X can be placed before L (50) and C (100) to make 40 and 90. ⋅ C can be placed before D (500) and M (1000) to make 400 and 900.