A simple solution in C


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

  • 0
    B

    Explanation:
    Basically we use type conversion here from char to int.....
    sum=sum*26+(int)(*t)-(int)('A')+1;
    every time we go to a new character...The prevoius sum is multiplied by 26...


Log in to reply
 

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