C# - Solution


  • 0
    I

    '''

    public static class Solution {
    public static readonly Dictionary<char, int> RomanNumberDictionary;
    static Roman()
    {
    RomanNumberDictionary = new Dictionary<char, int>
    {
    { 'I', 1 },
    { 'V', 5 },
    { 'X', 10 },
    { 'L', 50 },
    { 'C', 100 },
    { 'D', 500 },
    { 'M', 1000 },
    };
    }

    public int RomanToInt(string s) {
         int total = 0;
            int current = 0; int previous = 0;
            char curRoman, prevRoman = '\0';
            for(int i = 0; i< roman.Length;i++)
            {
                curRoman = roman[i];
                previous = prevRoman == '\0' ? '\0' : RomanNumberDictionary[prevRoman];
                current = RomanNumberDictionary[curRoman];
    
                if (previous != 0 && current > previous)
                {
                    total = total - (2 * previous) + current;
                }
                else
                    total += current;
    
                prevRoman = curRoman;
            }
    
            return total;
    }
    

    }

    }
    '''


Log in to reply
 

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