C# solution using dictionary


  • 0
    C
    public class Solution {
        public int RomanToInt(string s) {
            string tempSt = s + 'N';
                int sum = 0;
                Dictionary<char, int> romanSet = new Dictionary<char, int>
                {
                    { 'I' , 1 },
                    { 'V' , 5 },
                    { 'X' , 10 },
                    { 'L' , 50 },
                    { 'C' , 100 },
                    { 'D' , 500 },
                    { 'M' , 1000 },
                    { 'N' , 0}
                };
                for (var i = 0; i < tempSt.Length - 1; i++)
                    sum += romanSet[tempSt[i]] - romanSet[tempSt[i + 1]] >= 0 ? romanSet[tempSt[i]] : (-romanSet[tempSt[i]]);
                return sum;      
        }
    }
    

Log in to reply
 

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