Why my code can be accepted ?


  • 0
    F

    int romanToInt(char* s) {
    int num[7] = { 0 }, i = 0, N = 0;
    char I, X, V, L, C, D, M;
    for (i = 0; i<strlen(s); i++) {
    if (s[i] == 'I') num[s[i]] = 1;
    if (s[i] == 'X') num[s[i]] = 10;
    if (s[i] == 'V') num[s[i]] = 5;
    if (s[i] == 'L') num[s[i]] = 50;
    if (s[i] == 'C') num[s[i]] = 100;
    if (s[i] == 'D') num[s[i]] = 500;
    if (s[i] == 'M') num[s[i]] = 1000;

    }
    
    for (i = 0; i<strlen(s); i++) {
    	if (num[s[i]]<num[s[i + 1]])
    		N = N - num[s[i]];
    	else N = N + num[s[i]];
    
    }
    
    return N;
    

    }


Log in to reply
 

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