My 4ms C & 8ms C++ solution in 3 lines


  • 1
    H
    int titleToNumber(char* s) {
        int ret = 0;
        for ( ; *s ; ret = 26 * ret + ( *s++ - 'A' + 1 ) ) ;
        return ret;
    }
    
    class Solution {
    public:
        int titleToNumber(string s) {
            int ret = 0;
            for ( const char *str = s.c_str(); *str ; ret = 26 * ret + ( *str++ - 'A' + 1 ) ) ;
            return ret;
        }
    };

Log in to reply
 

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