simple c# solution, easy to understand


  • 0

    public class Solution {

    public int RomanToInt(string s) {
        if(s==null||s.Length==0)return 0;
        Dictionary<char,int> dic=getDic();
        int sum=dic[s[0]];
        for(int i=1;i<s.Length;i++)
        {
            sum+=dic[s[i-1]]<dic[s[i]]?dic[s[i]]-2*dic[s[i-1]]:dic[s[i]];
        }
        return sum;
    }
    
    private Dictionary<char,int> getDic()
    {
        Dictionary<char,int> dic=new Dictionary<char,int>();
        dic.Add('I',1);
        dic.Add('V',5);
        dic.Add('X',10);
        dic.Add('L',50);
        dic.Add('C',100);
        dic.Add('D',500);
        dic.Add('M',1000);
        return dic;
    }
    

    }


Log in to reply
 

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