C# solution: iterate from the back to the front


  • 0
    B
    public class Solution 
    {
        public int RomanToInt(string s) 
        {
            var n = s.Length;
            var romanAndInt = new Dictionary<char, int>()
            {
                {'I', 1}, 
                {'V', 5},
                {'X', 10},
                {'L', 50},
                {'C', 100},
                {'D', 500},
                {'M', 1000}
            };
    
            var result = romanAndInt[s[n - 1]];
    
            for(int i = s.Length - 2; i >= 0; i--)
            {
                var cur = romanAndInt[s[i]];
                var post = romanAndInt[s[i+1]];
    
                if (cur < post)
                {
                    result -= cur;
                }
                else
                {
                    result += cur;
                }
            }
    
            return result;
        }
    }
    

Log in to reply
 

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