public class Solution { public int RomanToInt(string s) { if(string.IsNullOrEmpty(s)) { return 0; } var len = s.Length; var result = Convert(s[0]); if(len < 2){ return result; } for(var i = 1;i < len; i++){ var pre = Convert(s[i - 1]); var current = Convert(s[i]); if(current <= pre){ result += current; }else{ result = result - 2 * pre + current; } } return result; } private int Convert(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 : throw new ArgumentException(); } } }