Simple,easy to understand C solution


  • 0
    M
    int romanToInt(char* s) {
        int a[32],k,i,j=0;
        for(i=0;s[i]!='\0';i++)
        {
            if(s[i]=='I')
            a[i]=1;
            else if(s[i]=='V')
            a[i]=5;
            else if(s[i]=='X')
            a[i]=10;
            else if(s[i]=='L')
            a[i]=50;
            else if(s[i]=='C')
            a[i]=100;
            else if(s[i]=='D')
            a[i]=500;
            else
            a[i]=1000;
            j++;
        }
        k=a[j-1];
        for(i=j-1;i>0;i--)
        {
            if(a[i]>a[i-1])
            k=k-a[i-1];
            else
            k=k+a[i-1];
        }
        return k;
    }

Log in to reply
 

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