My very simple C# code - Accepted ,, your comments is more than welcomed


  • 1
    N
    public int RomanToInt(string s) 
    
    {
    
        if (s.Length == 0)
        return 0;
    
        Hashtable ht = new Hashtable();
    
        ht.Add('I',1);
        ht.Add('V',5);
        ht.Add('X',10);
        ht.Add('L',50);
        ht.Add('C',100);
        ht.Add('D',500);
        ht.Add('M',1000);
        
        int i =0;
        int number = 0;
        while (i < s.Length)
    {
        if(i+1 < s.Length && (int)ht[char.ToUpper(s[i])] < (int)ht[char.ToUpper(s[i+1])])
        {
            number+= (int)ht[char.ToUpper(s[i+1])] - (int)ht[char.ToUpper(s[i])];
        i+=2;
        }
        
        else
        {
        number += (int)ht[char.ToUpper(s[i])]; 
            i++;
        }
    }
    return number;
    }

Log in to reply
 

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