My c solutions in 6ms and how can I improve my code to make it more concise?


  • 0
    S
    int titleToNumber(char *s) {
    int val, count, len;
    
    val = 0;    // initinal value
    count = 1;  // 26 hexadecimal
    len = 0;   // length of the string
    //  find length
    while(*s != '\0'){
        len += 1; 
        s++;   
    }
    while(len--){
        val += (*--s - 'A' + 1) * count;
        count *= 26;
    }
    return val;
    

    }


  • 0
    A

    Below is my 4ms C solution.

    int titleToNumber(char* s) {
        int result = 0;
        while (*s) {
            result = result * 26 + *s - 'A' + 1;
            s++;
        }
        return result;
    }

Log in to reply
 

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