my 0ms c solution ,easy to understand


  • 1
    S

    '''
    int romanToInt(char* s) {
    inline int rtoi(char ch){
    switch (ch){
    case 'I': return 1;
    case 'V': return 5;
    case 'X': return 10;
    case 'L': return 50;
    case 'C': return 100;
    case 'D': return 500;
    case 'M':return 1000;
    }
    return 0;
    }

    int i = strlen(s)-1;
    int j = 0;
    int sum = rtoi(s[i]);
    for (j = i; j > 0; j--){
    	if (rtoi(s[j]) > rtoi(s[j - 1])) sum -= rtoi(s[j - 1]);
    	else sum += rtoi(s[j - 1]);
    }
    return sum;
    

    }
    '''


Log in to reply
 

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